【问题标题】:Python Pandas v0.18+: is there a way to resample a dataframe without filling NAs?Python Pandas v0.18+:有没有办法在不填充 NA 的情况下重新采样数据帧?
【发布时间】:2016-07-22 10:41:23
【问题描述】:

我想知道是否有一种方法可以对 DataFrame 进行重新采样,而无需决定如何立即填充 NA。

我尝试了以下方法,但收到了未来警告:

FutureWarning:.resample() 现在是一个延迟操作,使用 .resample(...).mean() 而不是 .resample(...)

代码:

import pandas as pd
dates = pd.date_range('2015-01-01', '2016-01-01', freq='BM')
dummy = [i for i in range(len(dates))]
df = pd.DataFrame({'A': dummy})
df.index = dates
df.resample('B')

有没有更好的方法来做到这一点,不显示警告?

谢谢。

【问题讨论】:

    标签: python pandas dataframe nan resampling


    【解决方案1】:

    使用Resampler.asfreq:

    print (df.resample('B').asfreq())
                   A
    2015-01-30   0.0
    2015-02-02   NaN
    2015-02-03   NaN
    2015-02-04   NaN
    2015-02-05   NaN
    2015-02-06   NaN
    2015-02-09   NaN
    2015-02-10   NaN
    2015-02-11   NaN
    2015-02-12   NaN
    2015-02-13   NaN
    2015-02-16   NaN
    2015-02-17   NaN
    2015-02-18   NaN
    2015-02-19   NaN
    2015-02-20   NaN
    2015-02-23   NaN
    2015-02-24   NaN
    2015-02-25   NaN
    2015-02-26   NaN
    2015-02-27   1.0
    2015-03-02   NaN
    2015-03-03   NaN
    2015-03-04   NaN
    ...
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2018-09-11
      • 2018-02-01
      • 1970-01-01
      • 2017-07-07
      • 2022-12-05
      • 2020-11-29
      • 2013-01-31
      • 1970-01-01
      相关资源
      最近更新 更多