您应该能够将对象列转换为日期时间列,然后使用内置的 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
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'))
strftime
调用中漏掉了CheckOut_date
的日期。 - undefined