我有一个通过列表构建的数据框,我正在尝试确定可能丢失的蜡烛。当发现丢失的蜡烛时,我想在Pandas数据框中插入新行,其前一天的OHLC值(行)且音量设置为0。
list = [[1528992000000,
9.462e-05,
0.00010814,
9.202e-05,
0.00010544,
4600204.415809431],
[1529164800000,
0.00010309,
0.00010529,
0.0001016,
0.00010162,
1987989.1357407586],
[1529251200000,
0.00010165,
0.00010173,
9.402e-05,
9.508e-05,
1724979.853516945]]
df = pd.DataFrame(list)
df.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
df.set_index('timestamp', inplace = True)
df.index = pd.to_datetime( df.index, utc = True, unit = 'ms')
In [627]: df
Out[627]:
open high low close \
timestamp
2018-06-14 16:00:00+00:00 0.000095 0.000108 0.000092 0.000105
2018-06-16 16:00:00+00:00 0.000103 0.000105 0.000102 0.000102
2018-06-17 16:00:00+00:00 0.000102 0.000102 0.000094 0.000095
volume
timestamp
2018-06-14 16:00:00+00:00 4.600204e+06
2018-06-16 16:00:00+00:00 1.987989e+06
2018-06-17 16:00:00+00:00 1.724980e+06
在此示例中,缺少蜡烛2018-06-15 16:00:00+00:00
,我想重新创建一个这样的数据框。我该如何实现?
open high low close \
timestamp
2018-06-14 16:00:00+00:00 0.000095 0.000108 0.000092 0.000105
2018-06-15 16:00:00+00:00 0.000095 0.000108 0.000092 0.000105
2018-06-16 16:00:00+00:00 0.000103 0.000105 0.000102 0.000102
2018-06-17 16:00:00+00:00 0.000102 0.000102 0.000094 0.000095
volume
timestamp
2018-06-14 16:00:00+00:00 4.600204e+06
2018-06-15 16:00:00+00:00 0
2018-06-16 16:00:00+00:00 1.987989e+06
2018-06-17 16:00:00+00:00 1.724980e+06
因此,基本上,我可以通过将索引与涵盖该时间段的日期时间序列进行比较来识别丢失的索引,然后选择每个丢失的蜡烛的上一行,并创建包含所需数据的列表new
。
我的问题是我无法弄清楚将列表插入数据框的最佳方法是什么。我怎样才能做到这一点 ?
# Create sequence
start = pd.to_datetime( list[0][0], utc = True, unit = 'ms')
end = pd.to_datetime( list[-1][0], utc = True, unit = 'ms')
sequence = pd.date_range(start, end)
# Compare sequence
diff = sequence.difference(df.index)
if len(diff) != 0 :
for i in diff :
prev = i + datetime.timedelta( days = -1 )
row = df.loc[pd.Timestamp(prev)] # select previous row
new = [row[0], row[1], row[2], row[3], 0] # create desired data
# Doesn't return an error but failed to insert the new row
df.loc[i] = new
#df.loc[pd.Timestamp(i)] = new
答案 0 :(得分:1)
您可以使用asfreq将缺少的日期直接添加到数据框中:
df = df.asfreq('D')
要添加前一天的值,可以使用fillna:
df = df.fillna(method='ffill')
如果由于某些原因需要保留卷,则:
df = df.asfreq('D')
cols = ['open','high','low', 'close'] # list of columns to update
df[cols] = df[cols].fillna(method='ffill')
对于以前缺少的日期,卷将为NaN。如果需要0,也可以使用update:
df.update(df['volume'].fillna(0))