computing the mean for python datetime

前端 未结 5 585
梦谈多话
梦谈多话 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条回答
  •  自闭症患者
    2020-12-21 01:15

    Datetime math supports some standard operations:

    a = datetime.datetime(2014, 7, 9)
    b = datetime.datetime(2014, 7, 15)
    c = (b - a)/2
    
    # here c will be datetime.timedelta(3)
    
    a + c
    Out[7]: datetime.datetime(2014, 7, 12, 0, 0)
    

    So you can write a function that, given two datetimes, subtracts the lesser form the greater and adds half of the difference to the lesser. Apply this function to your dataframe, and shazam!

提交回复
热议问题