【问题标题】:shortcut for filling missing dates填写缺失日期的快捷方式
【发布时间】:2016-02-10 08:59:05
【问题描述】:

我有以下例子:

import numpy as np
import pandas as pd

idx1 = pd.period_range('2015-01-01', freq='10T', periods=1000)

idx2 = pd.period_range('2016-01-01', freq='10T', periods=1000)

df1 = pd.DataFrame(np.random.randn(1000), index=idx1, 
                   columns=['A'])
df2 = pd.DataFrame(np.random.randn(1000), index=idx2, 
                   columns=['A'])

frames = [df1, df2]

df_concat = pd.concat(frames)

现在,我想知道 df_concat 中缺失日期的数量

所以我填写了日期并重新索引了数据框:

start_total = df1.index[0]
end_total = df2.index[-1]
idx_total = pd.period_range(start=start_total, end=end_total, freq='10T')
df_total = df_concat.reindex(idx_total, fill_value=np.nan)
df_miss = df_total[df_total.isnull()]

最后的代码段有更短的版本吗?

类似df_concat.fill_missing_dates 的东西? 这是由 timeseries scikit 提供的: scikits.timeseries.TimeSeries.fill_missing_dates

【问题讨论】:

  • 也许可以帮助print df_concat.resample('10T')
  • df.fill_missing_dates(fill_value=...) 等价于 df.fillna(value=...)(如果 NaN 已经存在并且您不更改频率),或df.resample(freq).fillna(value)

标签: python pandas time-series missing-data


【解决方案1】:

我觉得你可以用resample:

df_total = df_concat.resample('10T')
print df_total[df_total.isnull()] 

                     A
2015-01-01 00:00:00 NaN
2015-01-01 00:10:00 NaN
2015-01-01 00:20:00 NaN
2015-01-01 00:30:00 NaN
2015-01-01 00:40:00 NaN
2015-01-01 00:50:00 NaN
2015-01-01 01:00:00 NaN
2015-01-01 01:10:00 NaN
2015-01-01 01:20:00 NaN
2015-01-01 01:30:00 NaN
2015-01-01 01:40:00 NaN
2015-01-01 01:50:00 NaN
2015-01-01 02:00:00 NaN
2015-01-01 02:10:00 NaN
2015-01-01 02:20:00 NaN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2018-10-05
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多