重置大 pandas 时间戳的时间部分

编程入门 行业动态 更新时间:2024-10-25 20:25:01
本文介绍了重置大 pandas 时间戳的时间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何重置大熊猫时间戳的时间部分?

How can I reset the time part of a pandas timestamp?

我想重新设定pandas.Timestamp的时间部分。 我想我可以使用以下过程来执行。

I want to reset time part in value of pandas.Timestamp. I guess I can do it using the following procedure.

  • 步骤1)到datetime类型的时间戳
  • 步骤2)datetime到秒
  • 步骤3)截断时间以秒为单位
  • 步骤4)带回秒针到Timestamp
  • step 1) Timestamp to datetime type
  • step 2) datetime to seconds
  • step 3) truncate time part in seconds
  • step 4) bring back seconds to Timestamp

即使我的猜测是正确的,它需要太长的时间。 有一个直接的方法来实现这个目标?

Even if my guess is correct, it takes too long to do. Is there a straightforward way to achieve this goal?

在[371]:ts = pd.Timestamp('2014 / 11/12 13:35')

In [371]: ts = pd.Timestamp('2014/11/12 13:35')

在[372]中:ts

In [372]: ts

Out [372]:时间戳('2014-11-12 13:35:00')

Out[372]: Timestamp('2014-11-12 13:35:00')

在[373]:ts.hour = 0#< - 这是我正在尝试

In [373]: ts.hour = 0 # <-- this is what I am trying to do.

推荐答案

我想你正在寻找 方法(请参阅文档):

I think you are looking for the replace method (see docs):

In [18]: ts Out[18]: Timestamp('2014-11-12 13:35:00') In [19]: ts.replace(hour=0) Out[19]: Timestamp('2014-11-12 00:35:00')

这是继承自 datetime.datetime

如果要重置全职部分,请在中指定所有部分替换:

If you want to reset the full time part, you specify all parts in replace:

In [20]: ts.replace(hour=0, minute=0, second=0) Out[20]: Timestamp('2014-11-12 00:00:00')

还有一个 DatetimeIndex.normalize 方法,但这并不适用于个人时间戳(我打开一个问题: github/pydata/pandas/issues/8794 ):

There is also a DatetimeIndex.normalize method, but this isn't available on the individual Timestamps (I opened an issue for that: github/pydata/pandas/issues/8794):

In [21]: pd.DatetimeIndex([ts]).normalize()[0] Out[21]: Timestamp('2014-11-12 00:00:00')

更多推荐

重置大 pandas 时间戳的时间部分

本文发布于:2023-11-23 14:48:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621834.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!