将两个多级索引合并到pandas

时间:2017-07-13 12:25:02

标签: python pandas datetime

我有多级索引的数据帧。这里第一个索引是一年中的日期,第二个索引是半小时时间。

Date             
2010-07-01  00:00    1.250
            00:30    1.244
            01:00    1.256
            01:30    0.744
            02:00    0.019
            02:30    1.250
            03:00    0.069
            03:30    0.000
            04:00    0.000

我想将两个索引合并为一个时间戳,如下所示:

Date             
 2010-07-01 00:00    1.250
 2010-07-01 00:30    1.244
 2010-07-01 01:00    1.256
 2010-07-01 01:30    0.744
 2010-07-01 02:00    0.019
 2010-07-01 02:30    1.250
 2010-07-01 03:00    0.069
 2010-07-01 03:30    0.000
 2010-07-01 04:00    0.000

我尝试了以下代码:

import datetime as dt

dat=df1.index.get_level_values(0)[0]
stamp=df1.index.get_level_values(1)[0]

indx=[dt.datetime(dat,stamp) for dat,stamp in zip(*[df.index.get_level_values(i) for i in (0, 1)])]

但是我收到了以下错误:

TypeError: an integer is required (got type Timestamp)

有一种简单的方法吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

使用to_timedelta转换时间字符串:

dat=df1.index.get_level_values(0)
stamp=df1.index.get_level_values(1)
df1.index = dat + pd.to_timedelta(stamp + ':00')
print (df1.index)
DatetimeIndex(['2010-07-01 00:00:00', '2010-07-01 00:30:00',
               '2010-07-01 01:00:00', '2010-07-01 01:30:00',
               '2010-07-01 02:00:00', '2010-07-01 02:30:00',
               '2010-07-01 03:00:00', '2010-07-01 03:30:00',
               '2010-07-01 04:00:00'],
              dtype='datetime64[ns]', freq='30T')

map的另一种解决方案:

df1.index = pd.to_datetime(df1.index.map(lambda x: '{} {}'.format(x[0], x[1])))
print (df1.index)
DatetimeIndex(['2010-07-01 00:00:00', '2010-07-01 00:30:00',
               '2010-07-01 01:00:00', '2010-07-01 01:30:00',
               '2010-07-01 02:00:00', '2010-07-01 02:30:00',
               '2010-07-01 03:00:00', '2010-07-01 03:30:00',
               '2010-07-01 04:00:00'],
              dtype='datetime64[ns]', freq=None)