Search code examples
pythonpandasdataframesum

Total time in pandas column


I am working on data frame. One of my column is in datetime format. Data head will look like this

0   2022-01-18 15:26:35
1   2022-01-18 15:26:36
2   2022-01-18 15:26:37
3   2022-01-18 15:26:38
4   2022-01-18 15:26:39
Name: filename, dtype: datetime64[ns]

I have a huge data and there is break at some point, meaning, I may not have data at night time and I dont want that time to be added in the total time. I want the total time in hours, I tried this

totaltime = df["filename"].sum()
totaltime

And I got an error, 'DatetimeArray' with dtype datetime64[ns] does not support reduction 'sum'. Can anyone help me please ?


Solution

  • Use numpy.ptp:

    df = pd.read_clipboard(header=None, names=['filename'], sep='\s\s+')`
    
    times = pd.to_datetime(df['filename'])
    
    np.ptp(times)
    

    Output (pd.Timedelta)

    Timedelta('0 days 00:00:04')