Reputation: 7644
i have a pandas dataframe df:
id value mins
1 a 12.4
2 u 14.2
3 i 16.2
3 g 17.0
i have a datetime.datetime variable:
current_time = datetime.datetime(2018, 1, 2, 14, 0, 34, 628481)
i want to add new column in my df 'total_time'
such that for every row it add the delta value mins in current_time.
id value mins total_time
1 a 12.4 current_time+timedelta(minutes = 12.4)
2 u 14.2 and
3 i 16.2 so
3 g 17.0 on... for every row
i tried:
df['total_time' = current_time+timedelta(minutes = df['mins'].item())
but i got an error:
can only convert an array of size 1 to a Python scalar
Is there any other way of doing this?
i'm using datetime.datetime package
Upvotes: 2
Views: 984
Reputation: 863501
I think you need to_timedelta
with T
for minutes:
df['total_time'] = current_time + pd.to_timedelta(df['mins'], unit='T')
print (df)
id value mins total_time
0 1 a 12.4 2018-01-02 14:12:58.628481
1 2 u 14.2 2018-01-02 14:14:46.628481
2 3 i 16.2 2018-01-02 14:16:46.628481
3 3 g 17.0 2018-01-02 14:17:34.628481
Detail:
print (pd.to_timedelta(df['mins'], unit='T'))
0 00:12:24
1 00:14:12
2 00:16:12
3 00:17:00
Name: mins, dtype: timedelta64[ns]
Upvotes: 2