问题
Aim:
I would like to use Timedelta to add hours, in decimal format, to an existing timestamp. My current code is giving me an issue - probably because I don't know how to not create a list (been struggling for a while on how to address things). Heh.
I have a dataframe named 'df' that looks roughly like the following:
+---------------------+----------+
| Time | AddHours |
+---------------------+----------+
| 2019-11-13 09:30:00 | 3.177481 |
| 2019-11-13 09:30:00 | 2.752435 |
| 2019-11-13 09:30:00 | 3.129910 |
| 2019-11-13 09:30:00 | 3.078170 |
| 2019-11-13 09:30:00 | 2.105979 |
| 2019-11-13 09:30:00 | 3.086892 |
+---------------------+----------+
Field Data Types
Because it's probably useful to know the datatype for this exercise:
df.dtypes returns:
Time datetime64[ns]
AddHours float64 <-- The units are hours. Eg 3.1202 hours
Existing / Problem Code
Now - the part I'm having trouble with.
I currently have:
df['Time'] = df['Time'] + [pd.Timedelta(hours=ts) for ts in df['AddHours']]
This returns an error that I don't know how to overcome. I assume that "for ts in df['AddHours']" is returning a list, where really I just want to add df['Time'] + df['AddHours'] for each row in the dataframe, and that Timedelta allows me to treat AddHours as decimal hours.
Any bright ideas on where I'm going wrong, and how to fix it?
Error:
TypeError: unsupported operand type(s) for +: 'DatetimeIndex' and 'list'
回答1:
Use to_timedelta for convert to Timedeltas
s:
df['Time'] = df['Time'] + pd.to_timedelta(df['AddHours'], unit='H')
print (df)
Time AddHours
0 2019-11-13 12:40:38.931600 3.177481
1 2019-11-13 12:15:08.766000 2.752435
2 2019-11-13 12:37:47.676000 3.129910
3 2019-11-13 12:34:41.412000 3.078170
4 2019-11-13 11:36:21.524400 2.105979
5 2019-11-13 12:35:12.811200 3.086892
Your solution is possible by convert list to Index
, but I think slow if large DataFrame
:
df['Time'] = df['Time'] + pd.Index([pd.Timedelta(hours=ts) for ts in df['AddHours']])
来源:https://stackoverflow.com/questions/61006263/pandas-timedelta-to-add-decimal-hours-to-existing-timestamp