我有一个整数TimeSeries
,我想使用resample()
进行下采样。问题是,我有一些时期缺少的数据转换为NaN
。由于pandas不支持整型NA值,所以将整型转换为浮点数。
是否有可能使用fill_value
重新采样TimeSeries
以查找丢失的数据,就像我可以使用reindex(fill_value=0)
一样?我不想把整型转换成浮点数
>>> dates = (datetime(2013, 1, 1), datetime(2013,1,2), datetime(2013,3,1))
>>> s = Series([1,2,4],index=dates)
>>> s
2013-01-01 1
2013-01-02 2
2013-03-01 4
dtype: int64
>>> s.resample('M', how='sum')
2013-01-31 3
2013-02-28 NaN
2013-03-31 4
Freq: M, dtype: float64
# Desired output (doesn't work)
>>> s.resample('M', how='sum', fill_value=0)
2013-01-31 3
2013-02-28 0
2013-03-31 4
Freq: M, dtype: int64
您可以定义自己的函数来避免NaN
In [36]: def _sum(x):
....: if len(x) == 0: return 0
....: else: return sum(x)
....:
In [37]: s.resample('M', how=_sum)
Out[37]:
2013-01-31 3
2013-02-28 0
2013-03-31 3
Freq: M, dtype: int64