Home > OS >  How do you add an amount of time (column 1) to a date/time (column 2) in a pandas data frame as a ne
How do you add an amount of time (column 1) to a date/time (column 2) in a pandas data frame as a ne

Time:02-14

Here is what I tried so far, the context is that I have a set of scripts that run everyday. I can get a dump of the date and time it started and the duration, I need to calculate the end time for each line.

import pandas as pd

df = pd.read_csv("script.csv")
df['EOD_DATE'] = pd.to_datetime(df['EOD_DATE'], format='%d %m %Y')
df['START_TIME'] = pd.to_datetime(df['START_TIME'], format='%Y-%m-%d %H:%M:%S', errors='ignore')
df.index = df['ROW_NUM']
df['SCRIPT_DURATION'] = pd.to_datetime(df['SCRIPT_DURATION'], format='%H:%M:%S', errors='ignore')
sum_column = pd.to_timedelta(df['SCRIPT_DURATION'], errors='ignore')   df['START_TIME']
df["END_TIME"] = sum_column
print(df.head())
#TypeError: unsupported operand type(s) for  : 'Timedelta' and 'str'

csv input extract

CodePudding user response:

Is it what you need?

df = pd.DataFrame({
    "START_TIME": ["2018-01-01 20:51:54", "2018-01-03 07:06:21", "2018-01-04 23:59:59"],
    "SCRIPT_DURATION": ["00:00:00", "00:07:17", "01:59:00"]
})

df["END_TIME"] = pd.to_datetime(df['START_TIME'])   pd.to_timedelta(df['SCRIPT_DURATION'])

df:

         START_TIME SCRIPT_DURATION            END_TIME
2018-01-01 20:51:54        00:00:00 2018-01-01 20:51:54
2018-01-03 07:06:21        00:07:17 2018-01-03 07:13:38
2018-01-04 23:59:59        01:59:00 2018-01-05 01:58:59
  • Related