我正在尝试预填充类似于以下内容的数据框:
在示例中,我随机删除了一些行以突出挑战。我正在尝试*优雅地计算 dti 值。第一行中的 dti 值将为 0(即使第一行按照脚本被删除)但由于 dti 序列中出现间隙需要跳过缺失的行。一种合乎逻辑的方法是将 dt/delta 相除以创建一个唯一的整数来表示桶,但我尝试过的任何东西都感觉不到或看起来很优雅。
一些代码来帮助模拟问题:
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
start = datetime.now()
nin = 24
delta='4H'
df = pd.date_range( start, periods=nin, freq=deltadf, name ='dt')
# remove some random data points
frac_points = 8/24 # Fraction of points to retain
r = np.random.rand(nin)
df = df[r <= frac_points] # reduce the number of points
df = df.to_frame(index=False) # reindex
df['dti'] = ...
先感谢您,
万千封印
相关分类