computing the mean for python datetime

前端 未结 5 592
梦谈多话
梦谈多话 2020-12-21 00:32

I have a datetime attribute:

d = {
    \'DOB\': pd.Series([
        datetime.datetime(2014, 7, 9),
        datetime.datetime(2014, 7, 15),
        np.datetim         


        
5条回答
  •  猫巷女王i
    2020-12-21 01:23

    You can take the mean of Timedelta. So find the minimum value and subtract it from the series to get a series of Timedelta. Then take the mean and add it back to the minimum.

    dob = df_test.DOB
    m = dob.min()
    (m + (dob - m).mean()).to_pydatetime()
    
    datetime.datetime(2014, 7, 12, 0, 0)
    

    One-line

    df_test.DOB.pipe(lambda d: (lambda m: m + (d - m).mean())(d.min())).to_pydatetime()
    

    To @ALollz point

    I use the epoch pd.Timestamp(0) instead of min

    df_test.DOB.pipe(lambda d: (lambda m: m + (d - m).mean())(pd.Timestamp(0))).to_pydatetime()
    

提交回复
热议问题