More efficient way to subtract hours from datetime both stored in Pandas DF columns

Question:

I have a pandas data frame with 2 columns. One containing date time and one with an integer of hours 1 to 10 . I want create a new column where I subtract the integer hours from the date time. The data frame contains 300,000+ rows. My current solution takes upwards of ~10 minutes to run and I need a more efficient solution than the lambda function. I run this code daily so

#example df
df_data = pd.DataFrame({'Date_Time': ['1/1/2022 12:00', '1/7/2022 18:00', '1/3/2022 14:00'], 
                   'offset': [1, 2, 5]})

#subtract time    
df_data['Date_Time_New'] = df_data.apply(lambda x: x.Date_Time- pd.DateOffset(hours=x.offset), axis=1)
Asked By: user17099549

||

Answers:

This should be a bit better:

df['Date_Time_New'] = pd.to_datetime(df['Date_Time']) - pd.to_timedelta(df['offset'],"hours")
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.