具有 pandas 均值函数的日期时间对象 [英] Datetime objects with pandas mean function

查看:67
本文介绍了具有 pandas 均值函数的日期时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,所以如果这个问题没有任何意义,我会提前道歉. 我注意到,当我尝试使用日期时间对象格式为以下格式的日期数据对象计算熊猫数据框的平均值时:datetime.datetime(2014,7,10),它无法计算平均值,但似乎是能够毫无问题地计算同一数据帧的最小值和最大值.

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 a date time object formatted like this: datetime.datetime(2014, 7, 10), it can not calculate the mean value of it however it seems to be able to calculate the minimum and maximum value of that same data frame with out a problem.

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

df
Out[18]: 
      one        two    
   a    1 2014-07-09
   b    2 2014-07-10
   c    3 2014-07-11

df.min()
Out[19]: 
   one             1
   two    2014-07-09
dtype: object

df.mean()
Out[20]: 
   one    2
dtype: float64

我确实注意到min和max函数将所有列都转换为对象,而均值函数仅输出浮点数. 谁能向我解释为什么均值函数只能处理浮点数? 我还有另一种方法来获取带有日期时间对象的数据框的平均值吗? 我可以使用纪元时间(整数)来解决它,但是如果有直接的方法,它将非常方便.我使用Python 2.7

I did notice that the min and the max function converted all the columns to objects, where as the mean function only outputs floats. Could anyone explain to me why the mean function can only handle floats? Is there another way I to get the mean values of a data frame with a date time object? I can work around it by using epoch time (as integer), but it would be very convenient if there was a direct way. I use Python 2.7

我很感谢任何提示.

推荐答案

您可以使用datetime.timedelta

You can use datetime.timedelta

import functools
import operator

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

def avg_datetime(series):
    dt_min = series.min()
    deltas = [x-dt_min for x in series]
    return dt_min + functools.reduce(operator.add, deltas) / len(deltas)

print(avg_datetime(df['two']))

这篇关于具有 pandas 均值函数的日期时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆