Finding the mean and standard deviation of a timedelta object in pandas df

前端 未结 4 1555
太阳男子
太阳男子 2020-12-16 11:29

I would like to calculate the mean and standard deviation of a timedelta by bank from a dataframe with two columns shown

4条回答
  •  星月不相逢
    2020-12-16 12:14

    I would suggest passing the numeric_only=False argument to mean as mentioned by Alexander Usikov - this works for pandas version 0.20+.

    If you have an older version, the following works:

    import pandas pd
    
    df = pd.DataFrame({
        'td': pd.Series([pd.Timedelta(days=i) for i in range(5)]),
        'group': ['a', 'a', 'a', 'b', 'b']
    })
    
    (
        df
        .astype({'td': int})         # convert timedelta to integer (nanoseconds)
        .groupby('group')
        .mean()
        .astype({'td': 'timedelta64[ns]'})
    )
    

提交回复
热议问题