I have a pandas sim_df that looks like this:
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?
The to_timedelta() function is used to convert argument to datetime. Timedeltas are absolute differences in times, expressed in difference units (e.g. days, hours, minutes, seconds). This method converts an argument from a recognized timedelta format / value into a Timedelta type. The data to be converted to timedelta.
Using pandas datetime properties. Initially, the values in datetime are character strings and do not provide any datetime operations (e.g. extract the year, day of the week,…). By applying the to_datetime function, pandas interprets the strings and convert these to datetime (i.e. datetime64[ns, UTC] ) objects.
pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With