如何在不跳过pandas中的nan值的情况下重新采样

时间:2013-08-29 19:39:54

标签: python pandas

我正在尝试获取具有NaN值的10天数据汇总。如果10天持续时间内存在NaN值,则10天的总和应返回nan值。

当我应用以下代码时,pandas将NaN视为零,并将剩余天数的总和重新计算。

dateRange = pd.date_range(start_date, periods=len(data), freq='D')
# Creating a data frame so that the timeseries can handle numpy array.
df = pd.DataFrame(data)
base_Series = pd.DataFrame(list(df.values), index=dateRange)
# Converting to aggregated series
agg_series = base_Series.resample('10D', how='sum')
agg_data = agg_series.values 

示例数据:

2011-06-01  46.520536
2011-06-02   8.988311
2011-06-03   0.133823
2011-06-04   0.274521
2011-06-05   1.283360
2011-06-06   2.556313
2011-06-07   0.027461
2011-06-08   0.001584
2011-06-09   0.079193
2011-06-10   2.389549
2011-06-11        NaN
2011-06-12   0.195844
2011-06-13   0.058720
2011-06-14   6.570925
2011-06-15   0.015107
2011-06-16   0.031066
2011-06-17   0.073008
2011-06-18   0.072198
2011-06-19   0.044534
2011-06-20   0.240080

输出:

2011-06-01  62.254651
2011-06-11   7.301481

任何帮助都将不胜感激。

`

2 个答案:

答案 0 :(得分:1)

这使用numpy sum,如果总和中存在nan,则将返回nan

In [35]: s = Series(randn(100),index=date_range('20130101',periods=100))

In [36]: s.iloc[11] = np.nan

In [37]: s.resample('10D',how=lambda x: x.values.sum())
Out[37]: 
2013-01-01    6.910729
2013-01-11         NaN
2013-01-21   -1.592541
2013-01-31   -2.013012
2013-02-10    1.129273
2013-02-20   -2.054807
2013-03-02    4.669622
2013-03-12    3.489225
2013-03-22    0.390786
2013-04-01   -0.005655
dtype: float64

答案 1 :(得分:0)

过滤掉那些有NaN的日子,我建议你做

noNaN_days_only = s.groupby(lambda x: x.date).filter(lambda x: ~x.isnull().any()

其中sDataFrame