1

I am attempting to following the steps in Pandas: add timedelta column to datetime column (vectorized) to add a time delta column to my dataframe by converting a'pandas._libs.tslibs.timestamps.Timestamp' series.

Code:

df = pd.DataFrame([['2020-07-25 09:26:28',2],['2020-07-25 09:26:2',10],['2020-07-25 09:26:30',203],['2020-07-25 09:26:31',30]], 
                      columns = ['Time','Load'])

df['Time'] = pd.to_datetime(df['Time'])
print(df)
print(type(df["Time"][0]))
df_static['time_delta'] = pd.to_timedelta(df_static['Time'])

However, I get the following error:

TypeError: dtype datetime64[ns] cannot be converted to timedelta64[ns]

What am I doing wrong?

AHalperin
  • 49
  • 1
  • 9

1 Answers1

2

If need times from datetimes convert to timedeltas convert datetimes to format HH:MM:SS by Series.dt.strftime:

df['td'] = pd.to_timedelta(pd.to_datetime(df['Time']).dt.strftime('%H:%M:%S'))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252