如何在 Python 中将 DataFrame 的列更改为 DateTime?

How to change a column of a DataFrame to DateTime in Python?

我有以下 DataFrame,我想将列 'hour' 更改为 DateTime 格式,因为它已作为字符串引入。

import pandas as pd
import datetime
result=df_tott.copy()
result.head()

                popular_board_data                                        post_time_data                                         user_link                      username    hour    totalCount
0   {"boards":[{"postCount":"75","topicCount":"5",...   {"hours":[{"hour":"00:00","postCount":"12","to...   http://atariage.com/forums/user/31751-low-budg...   last        00:00   12
1   {"boards":[{"postCount":"351","topicCount":"11...   {"hours":[{"hour":"00:00","postCount":"79","to...   http://atariage.com/forums/user/4026-bomberpun...   truk        02:00   3
2   {"boards":[{"postCount":"2","topicCount":"2","...   {"hours":[{"hour":"00:00","postCount":0,"topic...   http://atariage.com/forums/user/62944-sergei27...   ets         00:00   0

我试过这段代码,但我得到了一个包含今天日期的完整 DateTime 格式。我不想要日期,只想要作为 DateTime 的小时。

result['date']=pd.to_datetime(result['hour'])
data=result.loc[:,['hour','totalCount']]
data=data.set_index(result.date)
data.head()

输出:

            hour    totalCount
 date       
2019-07-22  00:00   12
2019-07-22  02:00   3
2019-07-22  00:00   0

我想要类似的输出,但 'date' 列的外观与 'hour' 相似,但不是字符串:

            hour    totalCount
 hour_min       
 00:00      00:00   12
 02:00      02:00   3
 00:00      00:00   0

您应该首先在 python 中将日期时间转换为时间对象,然后从时间对象数组创建一个新的 Series 对象。

试试这个

result=df_tott.copy()
result.head()
result['date']=pd.Series([obj.time() for obj in pd.to_datetime(result['hour'])])
result.date.name = 'hour_min'
data=result.loc[:,['hour','totalCount']]
data=data.set_index(result.date)
data.head()