除非我完全误解了reindex
的用途,否则我认为下面的代码段应填写系列,并删除时间索引NaN
:
import pandas as pd
import numpy
dateRange = pd.date_range(start='2014-01-01 1:00:00', periods=8, freq='S')
modifiedRange = dateRange.values
modifiedRange = numpy.delete(modifiedRange, (2), axis=0) # remove third row
ts = pd.Series(range(len(modifiedRange)), index=modifiedRange) # series with a gap
print(ts)
ts.reindex(dateRange) # pad the gap with NaN
print(ts)
我得到的输出在这里:
2014-01-01 01:00:00 0
2014-01-01 01:00:01 1
2014-01-01 01:00:03 2
2014-01-01 01:00:04 3
2014-01-01 01:00:05 4
2014-01-01 01:00:06 5
2014-01-01 01:00:07 6
dtype: int64
2014-01-01 01:00:00 0
2014-01-01 01:00:01 1
2014-01-01 01:00:03 2
2014-01-01 01:00:04 3
2014-01-01 01:00:05 4
2014-01-01 01:00:06 5
2014-01-01 01:00:07 6
dtype: int64
两张照片相同......但我希望第二张照片包含填充的NaN值:
2014-01-01 01:00:00 0
2014-01-01 01:00:01 1
2014-01-01 01:00:02 NaN
2014-01-01 01:00:03 2
2014-01-01 01:00:04 3
2014-01-01 01:00:05 4
2014-01-01 01:00:06 5
2014-01-01 01:00:07 6
dtype: int64
答案 0 :(得分:2)
reindex
不在原位。这样做,你会得到你期望的。
ts = ts.reindex(dateRange)
print(ts)