将元组索引转换为日期时间索引

时间:2016-06-05 21:13:21

标签: python datetime pandas tuples

我按月/日/年等平均我的数据框,并且在我的索引从Datetime转换为Tuple时遇到了麻烦。我希望在Datetime中拥有我的索引,以便我可以将其导出到excel以供其他非python用户使用,并使它仍然有意义的时间戳。

这就是我的Df的样子:

Index   Date Time       Value
1       1/26/2016 07:00 100000.0    
2       1/26/2016 07:00 1000000.0   
3       1/26/2016 14:46     98.52
6       1/26/2016 14:46     Nan
8       1/26/2016 14:48     100.94
11      1/26/2016 14:48     Nan

这是我遇到的问题摘要:

df_cv_1_grouped = df_cv_1.set_index('Date Time',drop=False)
year_hour_means = df_cv_1_grouped.groupby(
    lambda x: (x.year, x.month, x.day, x.hour)).mean()

输出很棒,但索引现在是一个元组(“值”列无关紧要。)

Index               Value
(2016, 1, 26, 7)    1.5
(2016, 1, 26, 14)   22.7
(2016, 1, 26, 15)   125.3
(2016, 1, 26, 16)   288.5   

我似乎无法以简单的方式找到将其恢复到日期时间(或保留在那里)的方法。

2 个答案:

答案 0 :(得分:2)

我认为您可以将index to_periodgroupby转换为indexlevel=0),然后转换为to_timestamp

df_cv_1_grouped = df_cv_1.set_index('Date Time', drop=False)

df_cv_1_grouped = df_cv_1_grouped.to_period('H')
print (df_cv_1_grouped)
                           Date Time       Value
Date Time                                       
2016-01-26 07:00 2016-01-26 07:00:00   100000.00
2016-01-26 07:00 2016-01-26 07:00:00  1000000.00
2016-01-26 14:00 2016-01-26 14:46:00       98.52
2016-01-26 14:00 2016-01-26 14:46:00         NaN
2016-01-26 14:00 2016-01-26 14:48:00      100.94
2016-01-26 14:00 2016-01-26 14:48:00         NaN

year_hour_means1 = df_cv_1_grouped.groupby(level=0).mean()
print (year_hour_means1)
                      Value
Date Time                  
2016-01-26 07:00  550000.00
2016-01-26 14:00      99.73

print (year_hour_means1.index)
PeriodIndex(['2016-01-26 07:00', '2016-01-26 14:00'], 
dtype='int64', name='Date Time', freq='H')

year_hour_means1 = year_hour_means1.to_timestamp()
print (year_hour_means1)
                         Value
Date Time                     
2016-01-26 07:00:00  550000.00
2016-01-26 14:00:00      99.73

print (year_hour_means1.index)
DatetimeIndex(['2016-01-26 07:00:00', '2016-01-26 14:00:00'], 
dtype='datetime64[ns]', name='Date Time', freq=None)

Converting between representations

答案 1 :(得分:0)

首先,您应该使用pd.to_datetimeDate Time值转换为datetime,然后您可以使用set_index

df['Date Time'] = pd.to_datetime(df['Date Time'])
df2 = df.set_index('Date Time')