【问题标题】:Unable to resample between irregular 15 minutes time-stamp 00:14:59 - 00:29:59 in pandas无法在熊猫的不规则 15 分钟时间戳 00:14:59 - 00:29:59 之间重新采样
【发布时间】:2019-09-26 20:08:57
【问题描述】:

我有一个数据框“df”,其日期时间索引设置为 15 分钟间隔。间隔是这样的

Index                Data
2015-03-15 00:14:59  36.0
2015-03-15 00:29:59  54.9
2015-03-15 00:44:59  28.7

我想通过对数据进行插值,将上述数据按分钟间隔进行上采样,如下所示。

Index                Data
2015-03-15 00:14:59  36.0
2015-03-15 00:15:59  36.5
2015-03-15 00:16:59  43.3
...... so on

但是,我尝试了以下操作:

df = df.resample('T').interpolate(method='spline', order=3)

也试过了:

df = df.resample('60s').interpolate(method='spline', order=3)

但是,以上两种方法都会产生以下结果。他们将微秒设置为 00:00:00,而我希望根据以 59. 00:14:59 结束的起始时间戳进行设置。

Index                Data
2015-03-15 00:14:00  NaN
2015-03-15 00:15:00  NaN
2015-03-15 00:16:00  NaN
...... so on

最后如何按照 59 微秒的统一间隔进行上采样?

【问题讨论】:

    标签: python pandas indexing time-series resampling


    【解决方案1】:

    reindexinterpolate 一起使用

    df.reindex(pd.date_range(df.index.min(),df.index.max(),freq='T')).interpolate()
    

    【讨论】:

    • 不,它不起作用。仍在生产 NaN。仅在微秒日期 00:14:59 而不是 00:14:00 有数据可用。所以它会产生Nan。请正确阅读问题。我希望它的间隔时间为 00:14:59、00:15:59 @WeNYoBen
    【解决方案2】:

    WeNYoBen 的解决方案将在 Index 列已被索引的情况下以所需的时间间隔生成 valeus。如果没有,下面的公式将生成值。 pd.date_range(df['Index'].iloc[0],df['Index'].iloc[-1],freq='T') 这将提供如下值['2015-03-15 00:14:59', '2015-03-15 00:15:59', '2015-03-15 00:16:59', '2015-03-15 00:17:59', '2015-03-15 00:18:59', '2015-03-15 00:19:59', '2015-03-15 00:20:59', '2015-03-15 00:21:59', '2015-03-15 00:22:59', '2015-03-15 00:23:59', '2015-03-15 00:24:59', '2015-03-15 00:25:59', '2015-03-15 00:26:59', '2015-03-15 00:27:59', '2015-03-15 00:28:59', '2015-03-15 00:29:59', '2015-03-15 00:30:59', '2015-03-15 00:31:59', '2015-03-15 00:32:59', '2015-03-15 00:33:59', '2015-03-15 00:34:59', '2015-03-15 00:35:59', '2015-03-15 00:36:59', '2015-03-15 00:37:59', '2015-03-15 00:38:59', '2015-03-15 00:39:59', '2015-03-15 00:40:59', '2015-03-15 00:41:59', '2015-03-15 00:42:59', '2015-03-15 00:43:59', '2015-03-15 00:44:59'] 但重新索引对我不起作用!说不定你能猜出来。

    【讨论】:

      【解决方案3】:

      试试:

      df = df.asfreq('60s').interpolate()
      

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 2014-08-09
        • 2013-12-06
        • 2021-07-05
        • 1970-01-01
        • 2012-04-08
        • 2023-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多