我是靠谱客的博主 凶狠雪碧,最近开发中收集的这篇文章主要介绍python datatime 平均值_计算python 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)

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

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

解决方案

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

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

最后

以上就是凶狠雪碧为你收集整理的python datatime 平均值_计算python datetime的平均值的全部内容,希望文章能够帮你解决python datatime 平均值_计算python datetime的平均值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部