如何将时间序列数据索引替换为pandas中的其他值?

时间:2018-01-24 13:32:07

标签: pandas indexing time time-series

我有以下数据框。每天我都有9分。 日期,时间是多索引索引。 我想在其他时间(00:00:00~2:00:00)每天更换时间指数

Date        Time     a      b    c
2018-01-09  6:00:00 20.31   0   -2.95
2018-01-09  6:15:00 20.76   26738   -2.88
2018-01-09  6:30:00 21.4    22462   -2.77
2018-01-09  6:45:00 21.84   20033   -3
2018-01-09  7:00:00 22.17   20010   -3.28
2018-01-09  7:15:00 22.38   18133   -2.82
2018-01-09  7:30:00 22.75   18254   -3.14
2018-01-09  7:45:00 22.93   17039   -3.22
2018-01-09  8:00:00 23.13   15934   -3.27
2018-01-10  6:00:00 20.31   0   -2.95
2018-01-10  6:15:00 20.76   26738   -2.88
2018-01-10  6:30:00 21.4    22462   -2.77
2018-01-10  6:45:00 21.84   20033   -3
2018-01-10  7:00:00 22.17   20010   -3.28
2018-01-10  7:15:00 22.38   18133   -2.82
2018-01-10  7:30:00 22.75   18254   -3.14
2018-01-10  7:45:00 22.93   17039   -3.22
2018-01-10  8:00:00 23.13   15934   -3.27

所以结果应该低于

Date        Time     a      b    c
2018-01-09  0:00:00      20.31   0   -2.95
2018-01-09  0:15:00 20.76   26738   -2.88
2018-01-09  0:30:00 21.4    22462   -2.77
2018-01-09  0:45:00 21.84   20033   -3
2018-01-09  1:00:00 22.17   20010   -3.28
2018-01-09  1:15:00 22.38   18133   -2.82
2018-01-09  1:30:00 22.75   18254   -3.14
2018-01-09  1:45:00 22.93   17039   -3.22
2018-01-09  2:00:00 23.13   15934   -3.27
2018-01-10  0:00:00 20.31   0   -2.95
2018-01-10  0:15:00 20.76   26738   -2.88
2018-01-10  0:30:00 21.4    22462   -2.77
2018-01-10  0:45:00 21.84   20033   -3
2018-01-10  1:00:00 22.17   20010   -3.28
2018-01-10  1:15:00 22.38   18133   -2.82
2018-01-10  1:30:00 22.75   18254   -3.14
2018-01-10  1:45:00 22.93   17039   -3.22
2018-01-10  2:00:00 23.13   15934   -3.27

我该怎么做?

1 个答案:

答案 0 :(得分:1)

如果要将所有值按15min间隔时间替换天数,您可以先创建dictinary用于映射:

d = dict(enumerate(pd.date_range(start='2018-01-01', end='2018-01-02', freq='15T').strftime('%H:%M:%S')))
print (d)
{0: '00:00:00', 1: '00:15:00', 2: '00:30:00', 3: '00:45:00', 4: '01:00:00', 5: '01:15:00', 6: '01:30:00', 7: '01:45:00', 8: '02:00:00', 9: '02:15:00', 10: '02:30:00', 11: '02:45:00', 12: '03:00:00', 13: '03:15:00', 14: '03:30:00', 15: '03:45:00', 16: '04:00:00', 17: '04:15:00', 18: '04:30:00', 19: '04:45:00', 20: '05:00:00', 21: '05:15:00', 22: '05:30:00', 23: '05:45:00', 24: '06:00:00', 25: '06:15:00', 26: '06:30:00', 27: '06:45:00', 28: '07:00:00', 29: '07:15:00', 30: '07:30:00', 31: '07:45:00', 32: '08:00:00', 33: '08:15:00', 34: '08:30:00', 35: '08:45:00', 36: '09:00:00', 37: '09:15:00', 38: '09:30:00', 39: '09:45:00', 40: '10:00:00', 41: '10:15:00', 42: '10:30:00', 43: '10:45:00', 44: '11:00:00', 45: '11:15:00', 46: '11:30:00', 47: '11:45:00', 48: '12:00:00', 49: '12:15:00', 50: '12:30:00', 51: '12:45:00', 52: '13:00:00', 53: '13:15:00', 54: '13:30:00', 55: '13:45:00', 56: '14:00:00', 57: '14:15:00', 58: '14:30:00', 59: '14:45:00', 60: '15:00:00', 61: '15:15:00', 62: '15:30:00', 63: '15:45:00', 64: '16:00:00', 65: '16:15:00', 66: '16:30:00', 67: '16:45:00', 68: '17:00:00', 69: '17:15:00', 70: '17:30:00', 71: '17:45:00', 72: '18:00:00', 73: '18:15:00', 74: '18:30:00', 75: '18:45:00', 76: '19:00:00', 77: '19:15:00', 78: '19:30:00', 79: '19:45:00', 80: '20:00:00', 81: '20:15:00', 82: '20:30:00', 83: '20:45:00', 84: '21:00:00', 85: '21:15:00', 86: '21:30:00', 87: '21:45:00', 88: '22:00:00', 89: '22:15:00', 90: '22:30:00', 91: '22:45:00', 92: '23:00:00', 93: '23:15:00', 94: '23:30:00', 95: '23:45:00', 96: '00:00:00'}

然后将cumcount用于Countermap

s = df.groupby(level=0).cumcount().map(d)
print (s)
Date        Time   
2018-01-09  6:00:00    00:00:00
            6:15:00    00:15:00
            6:30:00    00:30:00
            6:45:00    00:45:00
            7:00:00    01:00:00
            7:15:00    01:15:00
            7:30:00    01:30:00
            7:45:00    01:45:00
            8:00:00    02:00:00
2018-01-10  6:00:00    00:00:00
            6:15:00    00:15:00
            6:30:00    00:30:00
            6:45:00    00:45:00
            7:00:00    01:00:00
            7:15:00    01:15:00
            7:30:00    01:30:00
            7:45:00    01:45:00
            8:00:00    02:00:00

最后为set_indexget_level_values重新分配新索引,用于第一级索引:

df = df.set_index([df.index.get_level_values(0), s])
print (df)
                         a      b     c
Date                                   
2018-01-09 00:00:00  20.31      0 -2.95
           00:15:00  20.76  26738 -2.88
           00:30:00  21.40  22462 -2.77
           00:45:00  21.84  20033 -3.00
           01:00:00  22.17  20010 -3.28
           01:15:00  22.38  18133 -2.82
           01:30:00  22.75  18254 -3.14
           01:45:00  22.93  17039 -3.22
           02:00:00  23.13  15934 -3.27
2018-01-10 00:00:00  20.31      0 -2.95
           00:15:00  20.76  26738 -2.88
           00:30:00  21.40  22462 -2.77
           00:45:00  21.84  20033 -3.00
           01:00:00  22.17  20010 -3.28
           01:15:00  22.38  18133 -2.82
           01:30:00  22.75  18254 -3.14
           01:45:00  22.93  17039 -3.22
           02:00:00  23.13  15934 -3.27