使用大 pandas 更加有效的使用日期时间戳的方式 [英] More efficient way to round to day timestamps using pandas

查看:124
本文介绍了使用大 pandas 更加有效的使用日期时间戳的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将一些时间戳导入熊猫框架(通过 MongoDBclient )。它们是微秒。我想要一个回合的方式。我曾经看到过一个关于使用 np.round 而转换为int并再次返回的问题,但这不起作用(我尝试将一个div加入3600 x 24 x 100000但是没有工作)。

I have imported some timestamps into a Pandas frame (via MongoDBclient). They are to the microseconds. I'd like a way to round to the Day. I've seen a previous question about using np.round while converting to ints and back again, but this doesn't work (I tried inlining a div by 3600 x 24 x 100000 but that didn't work).

我有这个比较简单的版本,但是看起来效率很低。在 to_datetime np.round 示例中我缺少什么。

I have this rather plain version, but it seems REALLY inefficient. What am I missing in either to_datetime, or the np.round example.

df['doa'] = df['doa'].map(lambda x: x.strftime("%Y-%m-%d")
pd.to_datetime(df['doa'])

注意,这些不是INDEXES,所以我不能使用频率技巧。

Note, these are not INDEXES so I can't use the frequency trick.

推荐答案

有这个功能请求,这表明没有好的方法:
ENH:将舍入方法添加到DatetimeIndex / TimedeltaIndex

There's this feature request, which suggests there's no good way: ENH: add rounding method to DatetimeIndex/TimedeltaIndex

然而,在文章中,我发现还有这种分钟方式,我可以修改:

However, in the article I found there is also this approach for minutes, which I might be able to modify:

pd.DatetimeIndex(((dti.asi8 /(1e9 * 60))。round()* 1e9 * 60).astype(np.int64))

pd.DatetimeIndex(((dti.asi8/(1e9*60)).round()*1e9*60).astype(np.int64))

圆形熊猫时间戳记

这篇关于使用大 pandas 更加有效的使用日期时间戳的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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