从12小时制格式创建pandas DateTimeIndex

时间:2016-06-19 16:31:02

标签: python pandas

我正在寻找一种在熊猫中创建datetimeindex的方法。我的数据如下:

      Date        Time         AAA  
0  06/17/2016  03:00:00 PM    19.13 
1  06/17/2016  02:00:00 PM    19.13 
2  06/17/2016  01:00:00 PM    19.26  
3  06/17/2016  12:00:00 AM    19.28 
4  06/17/2016  11:00:00 AM    19.28 

我想获得的结果是:

                       AAA
Date
2016-06-17 15:00:00   19.16
2016-06-17 14:00:00   19.14
2016-06-17 13:00:00   19.18
2016-06-17 12:00:00   19.27
2016-06-17 11:00:00   19.27

我注意到如何有效地执行此操作,因为我的时间列使用12小时时钟格式。

2 个答案:

答案 0 :(得分:2)

您可以使用to_datetime执行此操作:

df
Out[38]: 
         Date         Time    AAA
0  06/17/2016  03:00:00 PM  19.13
1  06/17/2016  02:00:00 PM  19.13
2  06/17/2016  01:00:00 PM  19.26
3  06/17/2016  12:00:00 AM  19.28
4  06/17/2016  11:00:00 AM  19.28

In [39]: df['Date']=pd.to_datetime(df['Date']+ ' '+df['Time'])

In [40]: df
Out[40]: 
                 Date         Time    AAA
0 2016-06-17 15:00:00  03:00:00 PM  19.13
1 2016-06-17 14:00:00  02:00:00 PM  19.13
2 2016-06-17 13:00:00  01:00:00 PM  19.26
3 2016-06-17 00:00:00  12:00:00 AM  19.28
4 2016-06-17 11:00:00  11:00:00 AM  19.28

In [40]: df=df.drop(['Time','Date'],axis=1).set_index(df['Date'])

In [41]: df
Out[41]: 
                       AAA
Date                      
2016-06-17 15:00:00  19.13
2016-06-17 14:00:00  19.13
2016-06-17 13:00:00  19.26
2016-06-17 00:00:00  19.28
2016-06-17 11:00:00  19.28

答案 1 :(得分:2)

使用日期对象而不是解析字符串

df = pd.DataFrame([
        ['06/17/2016', '03:00:00 PM', 19.13],
        ['06/17/2016', '02:00:00 PM', 19.13],
        ['06/17/2016', '01:00:00 PM', 19.26],
        ['06/17/2016', '12:00:00 AM', 19.28],
        ['06/17/2016', '11:00:00 AM', 19.28],
    ],
    columns=['Date', 'Time', 'AAA'],
)
df.Date = pd.to_datetime(df.Date)
df.Time = pd.to_datetime(df.Time) - pd.DatetimeIndex(df.Time).date

df.set_index(df.Date + df.Time)[['AAA']]

                       AAA
2016-06-17 15:00:00  19.13
2016-06-17 14:00:00  19.13
2016-06-17 13:00:00  19.26
2016-06-17 00:00:00  19.28
2016-06-17 11:00:00  19.28