Python 熊猫';s使用fill_方法重新采样:需要知道从哪个行复制的数据吗?

Python 熊猫';s使用fill_方法重新采样:需要知道从哪个行复制的数据吗?,python,pandas,Python,Pandas,我正在尝试使用重采样方法来填补timeseries数据中的空白。但是我也想知道哪一行被用来填充丢失的数据 这是我的输入系列 In [28]: data Out[28]: Date 2002-09-09 233.25 2002-09-11 233.05 2002-09-16 230.25 2002-09-18 230.10 2002-09-19 230.05 Name: Price 通过重采样,我将得到这个 In [29]: data.resample("D",

我正在尝试使用重采样方法来填补timeseries数据中的空白。但是我也想知道哪一行被用来填充丢失的数据

这是我的输入系列

In [28]: data
Out[28]: 
Date
2002-09-09    233.25
2002-09-11    233.05
2002-09-16    230.25
2002-09-18    230.10
2002-09-19    230.05
Name: Price
通过重采样,我将得到这个

In [29]: data.resample("D", fill_method='bfill')
Out[29]: 
Date
2002-09-09    233.25
2002-09-10    233.05
2002-09-11    233.05
2002-09-12    230.25
2002-09-13    230.25
2002-09-14    230.25
2002-09-15    230.25
2002-09-16    230.25
2002-09-17    230.10
2002-09-18    230.10
2002-09-19    230.05
Freq: D
我在找

Out[29]: 
Date
2002-09-09    233.25  2002-09-09
2002-09-10    233.05  2012-09-11
2002-09-11    233.05  2012-09-11
2002-09-12    230.25  2012-09-16
2002-09-13    230.25  2012-09-16
2002-09-14    230.25  2012-09-16
2002-09-15    230.25  2012-09-16
2002-09-16    230.25  2012-09-16
2002-09-17    230.10  2012-09-18  
2002-09-18    230.10  2012-09-18
2002-09-19    230.05  2012-09-19

有什么帮助吗?

系列
转换为
数据帧
后,将索引复制到它自己的列中。(
DatetimeIndex.format()
在这里很有用,因为它返回索引的字符串表示形式,而不是Timestamp/datetime对象。)

对于无聚合的重采样,有一个助手方法
asfreq()

这实际上是以下内容的简写,在中间
DataFrameGroupBy
对象上调用
last()

In [529]: df.resample("D", how='last', fill_method='bfill')
Out[529]: 
             Price    OrigDate
Date                          
2002-09-09  233.25  2002-09-09
2002-09-10  233.05  2002-09-11
2002-09-11  233.05  2002-09-11
2002-09-12  230.25  2002-09-16
2002-09-13  230.25  2002-09-16
2002-09-14  230.25  2002-09-16
2002-09-15  230.25  2002-09-16
2002-09-16  230.25  2002-09-16
2002-09-17  230.10  2002-09-18
2002-09-18  230.10  2002-09-18
2002-09-19  230.05  2002-09-19
In [528]: df.asfreq("D", method='bfill')
Out[528]: 
             Price    OrigDate
2002-09-09  233.25  2002-09-09
2002-09-10  233.05  2002-09-11
2002-09-11  233.05  2002-09-11
2002-09-12  230.25  2002-09-16
2002-09-13  230.25  2002-09-16
2002-09-14  230.25  2002-09-16
2002-09-15  230.25  2002-09-16
2002-09-16  230.25  2002-09-16
2002-09-17  230.10  2002-09-18
2002-09-18  230.10  2002-09-18
2002-09-19  230.05  2002-09-19
In [529]: df.resample("D", how='last', fill_method='bfill')
Out[529]: 
             Price    OrigDate
Date                          
2002-09-09  233.25  2002-09-09
2002-09-10  233.05  2002-09-11
2002-09-11  233.05  2002-09-11
2002-09-12  230.25  2002-09-16
2002-09-13  230.25  2002-09-16
2002-09-14  230.25  2002-09-16
2002-09-15  230.25  2002-09-16
2002-09-16  230.25  2002-09-16
2002-09-17  230.10  2002-09-18
2002-09-18  230.10  2002-09-18
2002-09-19  230.05  2002-09-19