Luis Miguel
Luis Miguel

Reputation: 5127

Using datetime timedelta with a series in a pandas DF

I have a pandas sim_df that looks like this:

enter image description here

Now, I want to add another column, "date" that is the date corresponding to 'now' plus 'cum_days' (a delta time).

start = dt.datetime.now()
sim_df['date'] = start + dt.timedelta(sim_df['cum_days'])

But it looks like deltatime does not use a series, but a fixed scalar.

TypeError: unsupported type for timedelta days component: Series

Is there a way to solve this in a vectorized operation without iterating over each row of sim_df?

Upvotes: 14

Views: 18781

Answers (3)

EdChum
EdChum

Reputation: 394041

construct a TimedeltaIndex from your column and add this to the scalar value:

In [26]:
sim_df = pd.DataFrame({'delta_time_days': [1.02, .09, 1.08, 1.7, 4.1, 0.3, .13, .01, .3, .7], 
                       'cum_days': [1.1, 1.1, 2.2, 3.9, 8.0, 8.3, 8.4, 8.4, 8.8, 9.5]})
start = dt.datetime.now()
sim_df['date'] = start + pd.TimedeltaIndex(sim_df['cum_days'], unit='D')
sim_df

Out[26]:
   cum_days  delta_time_days                       date
0       1.1             1.02 2016-02-12 01:40:32.413413
1       1.1             0.09 2016-02-12 01:40:32.413413
2       2.2             1.08 2016-02-13 04:04:32.413413
3       3.9             1.70 2016-02-14 20:52:32.413413
4       8.0             4.10 2016-02-18 23:16:32.413413
5       8.3             0.30 2016-02-19 06:28:32.413413
6       8.4             0.13 2016-02-19 08:52:32.413413
7       8.4             0.01 2016-02-19 08:52:32.413413
8       8.8             0.30 2016-02-19 18:28:32.413413
9       9.5             0.70 2016-02-20 11:16:32.413413

Upvotes: 7

Alexander
Alexander

Reputation: 109546

Add the timedelta to now using a list comprehension.

sim_df = pd.DataFrame({'delta_time_days': [1.02, .09, 1.08, 1.7, 4.1, 0.3, .13, .01, .3, .7], 
                       'cum_days': [1.1, 1.1, 2.2, 3.9, 8.0, 8.3, 8.4, 8.4, 8.8, 9.5]})

sim_df['date'] = [dt.datetime.now() + dt.timedelta(days=d) for d in sim_df.cum_days]

>>> sim_df
   cum_days  delta_time_days                       date
0       1.1             1.02 2016-02-11 17:36:11.320271
1       1.1             0.09 2016-02-11 17:36:11.320286
2       2.2             1.08 2016-02-12 20:00:11.320289
3       3.9             1.70 2016-02-14 12:48:11.320292
4       8.0             4.10 2016-02-18 15:12:11.320296
5       8.3             0.30 2016-02-18 22:24:11.320299
6       8.4             0.13 2016-02-19 00:48:11.320301
7       8.4             0.01 2016-02-19 00:48:11.320304
8       8.8             0.30 2016-02-19 10:24:11.320306
9       9.5             0.70 2016-02-20 03:12:11.320309

Upvotes: 4

Kris
Kris

Reputation: 23569

How about this?

start = dt.datetime.now()
sim_df['date'] = start + sim_df['cum_days'].map(dt.timedelta)

This applies dt.timedelta to each element of the cum_days column individually.

Upvotes: 18

Related Questions