Datetime objects with pandas mean function

前端 未结 3 1513
臣服心动
臣服心动 2020-12-11 04:00

I am new to programming so I apologize in advance if this question does not make any sens. I noticed that when I try to calculate the mean value of a pandas data frame with

3条回答
  •  失恋的感觉
    2020-12-11 04:32

    To simplify Alex's answer (I would have added this as a comment but I don't have sufficient reputation):

    import datetime
    import pandas as pd
    
    d={'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']), 
       'two': pd.Series([datetime.datetime(2014, 7, 9), 
               datetime.datetime(2014, 7, 10), 
               datetime.datetime(2014, 7, 11) ], 
               index=['a', 'b', 'c'])}
    df = pd.DataFrame(d)
    

    Which looks like:

       one   two
    a   1   2014-07-09
    b   2   2014-07-10
    c   3   2014-07-11
    

    Then calculate the mean of column "two" by:

    (df.two - df.two.min()).mean() + df.two.min()
    

    So, subtract the min of the timeseries, calculate the mean (or median) of the resulting timedeltas, and add back the min.

提交回复
热议问题