计算python datetime的平均值 [英] computing the mean for python datetime

查看:938
本文介绍了计算python datetime的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datetime属性:

I have a datetime attribute:

d = {
    'DOB': pd.Series([
        datetime.datetime(2014, 7, 9),
        datetime.datetime(2014, 7, 15),
        np.datetime64('NaT')
    ], index=['a', 'b', 'c'])
}
df_test = pd.DataFrame(d)

我想计算该属性的平均值.运行mean()会导致错误:

I would like to compute the mean for that attribute. Running mean() causes an error:

TypeError:此dtype不允许进行归约运算平均值"

TypeError: reduction operation 'mean' not allowed for this dtype

我还尝试了其他地方提出的解决方案.它无法运行,因为运行那里提出的功能会导致

I also tried the solution proposed elsewhere. It doesn't work as running the function proposed there causes

OverflowError:Python int太大,无法转换为C long

OverflowError: Python int too large to convert to C long

您会提出什么建议?以上数据框的结果应等于

What would you propose? The result for the above dataframe should be equivalent to

datetime.datetime(2014, 7, 12).

推荐答案

您可以采用Timedelta的平均值.因此,找到最小值并将其从序列中减去,即可得到一系列的Timedelta.然后取均值并将其加回到最小值.

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()


@ALollz点

我使用纪元pd.Timestamp(0)代替min

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

这篇关于计算python datetime的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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