Search code examples
pythonpandasdataframesum

sum time for dtype: object in df


I have object data in df in the 'czas_oczekiwania' column enter image description here

when grouping, I want to have the sum of times for individual days

ph3=ph1.groupby([pd.to_datetime(ph1['data']).dt.to_period('D'), 'typ'])['czas_oczekiwania'].sum()

but my result is enter image description here How can I change this?


Solution

  • i think you need datetime module and time converting with apply_dataframe

    import datetime
    
    result = (
        ph3
        .dt
        .components[["hours","minutes","seconds"]]
        .apply(lambda x: datetime.time(hour=x[0], minute=x[1], second=x[2]), axis=1)
    )
    result