简体   繁体   English

如何在 Pandas 数据框中组合 datetime.date 和 datetime.time 列?

[英]How can I combine datetime.date and datetime.time columns in pandas dataframe?

Given df给定 df

          Date      Time    Data     
3   2017-08-10  15:15:00    a    
0   2017-08-11  15:15:00    b    
1   2017-08-12  15:15:00    c    
2   2017-08-13  15:15:00    d    
1   2017-08-14  15:15:00    e    

And并且

print (type(df['Date'].iat[0]))
<class 'datetime.date'>

print (type(df['Time'].iat[0]))
<class 'datetime.time'>

How can I combine df.Date and df.Time into a DateTime column that is a datetime object ??:如何将 df.Date 和 df.Time 组合到作为日期时间对象的 DateTime 列中??:

        Date        Time    Data  DateTime   
3   2017-08-10  15:15:00    a     2017-08-10 15:15:00
0   2017-08-11  15:15:00    b     2017-08-11 15:15:00
1   2017-08-12  15:15:00    c     2017-08-12 15:15:00
2   2017-08-13  15:15:00    d     2017-08-13 15:15:00
1   2017-08-14  15:15:00    e     2017-08-14 15:15:00

What I tried:我试过的:

df['DateTime'] = df.apply(lambda r : pd.datetime.combine(r['Date'],r['Time']),1)

This works well as intended, However, I would prefer a vectorized operation and I get the following msg:这按预期工作得很好,但是,我更喜欢矢量化操作,并且得到以下消息:

C:\Users\User\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: 
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-
docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':

The issue here is that both date and time are already in datetime format.这里的问题是日期和时间都已经是日期时间格式。 Try试试

df['datetime'] = pd.to_datetime(df['Date'].dt.strftime('%Y-%m-%d') + df['Time'].astype(str), format = '%Y-%m-%d%H:%M:%S')

Though I don't know if it would be more efficient than using datetime.combine虽然我不知道它是否比使用 datetime.combine 更有效

I'm late to the party... Vaishali's answer works if "Date" is a datetime-like object.我迟到了……如果“日期”是一个类似日期时间的对象,那么 Vaishali 的回答就有效。

str(date) will give yyyy-mm-dd output according to docs , so you can do: str(date)根据 docs给出yyyy-mm-dd输出,因此您可以执行以下操作:

pd.to_datetime(df['Date'].astype(str) + df['Time'].astype(str), format = '%Y-%m-%d%H:%M:%S')

However, I have not tested this for speed.但是,我还没有测试过这个速度。

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

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