pandas 如何使用fill_value在pandas中重新采样TimeSeries?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16807836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resample a TimeSeries in pandas with a fill_value?
提问by gerdemb
I have a TimeSeriesof integers that I would like to downsample using resample(). The problem is that I have some periods with missing data that are converted to NaN. Since pandas does not support Integer NA valuesthe integers are converted to floats.
我有一个TimeSeries整数,我想使用resample(). 问题是我有一些丢失数据的时期被转换为NaN. 由于Pandas不支持整数 NA 值,因此整数被转换为浮点数。
Is it possible to resample a TimeSeriesusing a fill_valuefor missing data like I can with reindex(fill_value=0)? I don't want my integers cast into floats.
是否可以像我一样TimeSeries使用 afill_value对缺失数据重新采样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
回答by waitingkuo
You can define your own function to avoid NaN
您可以定义自己的函数来避免 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

