简体   繁体   English

在python熊猫中将时间对象转换为日期时间格式

[英]Convert time object to datetime format in python pandas

I have a dataset of column name DateTime having dtype object . 我有一个具有dtype object的列名DateTime的数据集。

df['DateTime'] = pd.to_datetime(df['DateTime'])

I have used the above code to convert to datetime format then did a split in the column to have Date and Time separately 我已经使用上面的代码将其转换为日期时间格式,然后在列中进行了拆分以分别具有日期时间

df['date'] = df['DateTime'].dt.date
df['time'] = df['DateTime'].dt.time

but after the split the format changes to object type and while converting it to datetime it showing error for the time column name as: TypeError: is not convertible to datetime 但是在拆分之后,格式更改为对象类型,并且在将其转换为日期时间时 ,将时间列名称显示为错误: TypeError:无法转换为日期时间

How to convert it to datetime format the time column 如何将其转换为日期时间格式的时间

You can use combine in list comprehension with zip : 您可以使用zip combine使用列表理解功能:

df = pd.DataFrame({'DateTime': ['2011-01-01 12:48:20', '2014-01-01 12:30:45']})
df['DateTime'] = pd.to_datetime(df['DateTime'])

df['date'] = df['DateTime'].dt.date
df['time'] = df['DateTime'].dt.time

import datetime
df['new'] = [datetime.datetime.combine(a, b) for a, b in zip(df['date'], df['time'])]
print (df)

             DateTime        date      time                 new
0 2011-01-01 12:48:20  2011-01-01  12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45  2014-01-01  12:30:45 2014-01-01 12:30:45

Or convert to strings, join together and convert again: 或转换为字符串,连接在一起然后再次转换:

df['new'] = pd.to_datetime(df['date'].astype(str) + ' ' +df['time'].astype(str))
print (df)
             DateTime        date      time                 new
0 2011-01-01 12:48:20  2011-01-01  12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45  2014-01-01  12:30:45 2014-01-01 12:30:45

But if use floor for remove times with converting times to timedeltas then use + only: 但是,如果将floor用于将时间转换为timedelta的删除时间,则只能使用+

df['date'] = df['DateTime'].dt.floor('d')
df['time'] = pd.to_timedelta(df['DateTime'].dt.strftime('%H:%M:%S'))

df['new'] = df['date'] + df['time']
print (df)

             DateTime       date     time                 new
0 2011-01-01 12:48:20 2011-01-01 12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45 2014-01-01 12:30:45 2014-01-01 12:30:45

How to convert it back to datetime format the time column 如何将其转换回日期时间格式的时间列

There appears to be a misunderstanding. 似乎有误会。 Pandas datetime series must include date and time components. 熊猫的datetime系列必须包含日期和时间部分。 This is non-negotiable. 这是不可谈判的。 You can simply use pd.to_datetime without specifying a date and use the default 1900-01-01 date: 你可以简单地使用pd.to_datetime没有指定具体的日期,并使用默认1900-01-01日期:

# date from jezrael

print(pd.to_datetime(df['time'], format='%H:%M:%S'))

0   1900-01-01 12:48:20
1   1900-01-01 12:30:45
Name: time, dtype: datetime64[ns]

Or use another date component, for example today's date: 或使用其他日期组件,例如今天的日期:

today = pd.Timestamp('today').strftime('%Y-%m-%d')
print(pd.to_datetime(today + ' '  + df['time'].astype(str)))

0   2018-11-25 12:48:20
1   2018-11-25 12:30:45
Name: time, dtype: datetime64[ns]

Or recombine from your date and time series: 或根据您的datetime序列重新组合:

print(pd.to_datetime(df['date'].astype(str) + ' ' + df['time'].astype(str)))

0   2011-01-01 12:48:20
1   2014-01-01 12:30:45
dtype: datetime64[ns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM