Pandas 在数据帧中解析日期时间

Pandas 在数据帧中解析日期时间,pandas,Pandas,我在Dataframe中有类型为“object”的签出列,格式为“2017-08-04T23:31:19.000+02:00”。 但我希望它的格式如图所示。 谁能帮帮我吗 谢谢:)我还没有测试过这个,但大致如下: df['CheckOut_date'] = pd.to_datetime(df["CheckOut_date"].dt.strftime('%Y-%m-%d')) df['CheckOut_time'] = pd.to_datetime(df["CheckOut_time"].dt

我在Dataframe中有类型为“object”的签出列,格式为“2017-08-04T23:31:19.000+02:00”。 但我希望它的格式如图所示。 谁能帮帮我吗


谢谢:)

我还没有测试过这个,但大致如下:

 df['CheckOut_date'] = pd.to_datetime(df["CheckOut_date"].dt.strftime('%Y-%m-%d'))
 df['CheckOut_time'] = pd.to_datetime(df["CheckOut_time"].dt.strftime('%H:%m:%s'))

我没有对此进行测试,但大致如下:

 df['CheckOut_date'] = pd.to_datetime(df["CheckOut_date"].dt.strftime('%Y-%m-%d'))
 df['CheckOut_time'] = pd.to_datetime(df["CheckOut_time"].dt.strftime('%H:%m:%s'))

您应该能够将对象列转换为日期-时间列,然后使用内置的
date
time
功能

# create an intermediate column that we won't store on the DataFrame
checkout_as_datetime = pd.to_datetime(df['checkout'])

# Add the desired columns to the dataframe
df['checkout_date'] = checkout_as_datetime.dt.date
df['checkout_time'] = checkout_as_datetime.dt.time
不过,如果您的目标不是将这些特定的新列写在某个地方,而是将它们用于其他计算,那么只覆盖原始列并从此处使用datetime方法可能会更简单

df['checkout'] = pd.to_datetime(df['checkout'])
df['checkout'].dt.date  # to access the date

您应该能够将对象列转换为日期-时间列,然后使用内置的
date
time
功能

# create an intermediate column that we won't store on the DataFrame
checkout_as_datetime = pd.to_datetime(df['checkout'])

# Add the desired columns to the dataframe
df['checkout_date'] = checkout_as_datetime.dt.date
df['checkout_time'] = checkout_as_datetime.dt.time
不过,如果您的目标不是将这些特定的新列写在某个地方,而是将它们用于其他计算,那么只覆盖原始列并从此处使用datetime方法可能会更简单

df['checkout'] = pd.to_datetime(df['checkout'])
df['checkout'].dt.date  # to access the date

您似乎错过了
strftime
调用
CheckOut\u date
中的一天。您似乎错过了
strftime
调用
CheckOut\u date
中的一天。