【问题标题】:how can i move time-series data from yesterday to today in pandas?如何在熊猫中将时间序列数据从昨天移动到今天?
【发布时间】:2018-07-15 01:36:04
【问题描述】:

我有跨天(12-02~1203)的数据框。我想每天将昨天的数据(12-02 22:00~00:00)移动到今天的数据(12-03)。日期/时间是多重索引的。这是我分析数据时需要的,它一天比一天更方便。但现在我需要分析包括昨天过去 2 小时在内的数据......所以我需要这个数据框操作。

..
 date         time       a     b 
2015-12-02  21:00:00    23.97   0
2015-12-02  21:15:00    24.06   0
2015-12-02  21:30:00    24.03   0
2015-12-02  21:45:00    23.99   0
2015-12-02  22:00:00    24.03   0
2015-12-02  22:15:00    23.89   0
2015-12-02  22:30:00    23.71   0
2015-12-02  22:45:00    23.64   0
2015-12-02  23:00:00    23.29   0
2015-12-02  23:15:00    23.8    0
2015-12-02  23:30:00    23.82   0
2015-12-02  23:45:00    23.86   0
2015-12-03  0:00:00 23.66   0
2015-12-03  0:15:00 23.64   0
2015-12-03  0:30:00 23.7    0
2015-12-03  0:45:00 23.69   0
2015-12-03  1:00:00 23.65   0
2015-12-03  1:15:00 23.48   0
2015-12-03  1:30:00 23.45   0
..

结果应该如下(12-02 22:00~23:45 数据移动到 12-03 我该怎么做?

..
2015-12-02  21:00:00    23.97   0
2015-12-02  21:15:00    24.06   0
2015-12-02  21:30:00    24.03   0
2015-12-02  21:45:00    23.99   0
2015-12-03  22:00:00    24.03   0
2015-12-03  22:15:00    23.89   0
2015-12-03  22:30:00    23.71   0
2015-12-03  22:45:00    23.64   0
2015-12-03  23:00:00    23.29   0
2015-12-03  23:15:00    23.8    0
2015-12-03  23:30:00    23.82   0
2015-12-03  23:45:00    23.86   0
2015-12-03  0:00:00 23.66   0
2015-12-03  0:15:00 23.64   0
2015-12-03  0:30:00 23.7    0
2015-12-03  0:45:00 23.69   0
2015-12-03  1:00:00 23.65   0
2015-12-03  1:15:00 23.48   0
2015-12-03  1:30:00 23.45   0
..

【问题讨论】:

    标签: python pandas time-series move


    【解决方案1】:

    我认为你需要:

    from datetime import date, datetime, time, timedelta
    
    m = df.index.get_level_values(1) < time(22,0,0)
    idx1 = df.index.get_level_values(0)
    idx2 = df.index.get_level_values(1)
    df.index = [idx1.where(m, idx1 +  timedelta(days=1)), idx2]
    
    print (df)
                             a  b
    date       time              
    2015-12-02 21:00:00  23.97  0
               21:15:00  24.06  0
               21:30:00  24.03  0
               21:45:00  23.99  0
    2015-12-03 22:00:00  24.03  0
               22:15:00  23.89  0
               22:30:00  23.71  0
               22:45:00  23.64  0
               23:00:00  23.29  0
               23:15:00  23.80  0
               23:30:00  23.82  0
               23:45:00  23.86  0
               00:00:00  23.66  0
               00:15:00  23.64  0
               00:30:00  23.70  0
               00:45:00  23.69  0
               01:00:00  23.65  0
               01:15:00  23.48  0
               01:30:00  23.45  0
    

    【讨论】:

      【解决方案2】:

      这种方式应该是有效的。首先提取每个时间的小时,然后在小时 >= 22 时增加一天。

      import pandas as pd
      from datetime import timedelta
      
      df['hour'] = pd.to_datetime(df['time'], format='%H:%M:%S').dt.hour
      df.loc[df['hour'] >=22, 'date'] = df['date'] +  timedelta(days=1)
      

      【讨论】:

      • 执行“df_per_wday.hour = pd.to_datetime(df_per_wday.time, format='%H:%M:%S').dt.hour”时出现错误'DataFrame'对象没有属性'时间'
      • 在我的数据帧数据中,时间是索引是否影响错误?
      • @jerryhan,现在试试代码。我已经使用df[col] 表示法明确标识了列。
      • 也尝试将索引提升到列:df = df.reset_index()
      • 这也有错误... pd.to_datetime(df['time'], format='%H:%M:%S').dt.hour 不执行... date时间是多索引...
      【解决方案3】:

      我不确定这是否是最快的方式,但您可以考虑使用np.where

      import numpy as np
      import pandas as pd
      
      df["date"] = pd.to_datetime(df["date"])
      offset = pd.DateOffset(days=1)
      df["date"] = np.where((df["time"]>="22:00") & (df["time"]<="23:45" ),
                            df["date"] + offset,
                            df["date"])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 2014-07-24
        • 2011-06-14
        • 2017-02-14
        • 1970-01-01
        • 2020-09-30
        相关资源
        最近更新 更多