Search code examples
pythonpandasdatabasedataframetimedelta

format the output from "x days xx:xx:xx" to "xx:xx" (timedelta64[ns] in minutes)


I have a dataframe, in which I created a new column referring to the duration between two dates:

df['test'] = ((df['data_1'] - df['data_2'])

out: 1 days 00:10:00

However, I need the output format to be:

34:00

Desired timedelta64[ns] in minutes


Solution

  • Could you provide a working example?

    Unfortunately, you cant access the total hours directly. However, what you can use is .total_seconds() and calculate the hours and minutes like this:

    import pandas as pd
    df = pd.DataFrame({'data_1': {0: '2022-09-22 00:00:00',
                                  1: '2022-09-22 00:00:00'},
                       'data_2': {0: '2022-09-23 00:01:00',
                                  1: '2022-09-24 05:00:00'}})
    df['data_1'] = pd.to_datetime(df['data_1'])
    df['data_2'] = pd.to_datetime(df['data_2'])
    df['test'] = df['data_2'] - df['data_1']
    
    
    def format_timedelta(td):
        return f"{td.days:02d}:{int(td.total_seconds()//3600):02d}"
    
    df['test'].apply(format_timedelta)
    

    or in one line using a lambda function:

    df['test'].apply(lambda row: f"{row.days:02d}:{int(row.total_seconds()//3600):02d}")