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)
CodePudding user response:
This should be a bit better:
df['Date_Time_New'] = pd.to_datetime(df['Date_Time']) - pd.to_timedelta(df['offset'],"hours")