Python 重置熊猫时间戳的时间部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26882499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:06:39  来源:igfitidea点击:

Reset time part of a pandas timestamp

pythondatetimepandastimestamp

提问by Hill

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

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

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

我想在 pandas.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
  • 步骤 1) 时间戳到日期时间类型
  • 步骤 2) 日期时间到秒
  • 步骤 3) 以秒为单位截断时间部分
  • 步骤 4)将秒数带回时间戳

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

即使我的猜测是正确的,也需要很长时间才能做到。有没有直接的方法来实现这个目标?

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

In [372]: ts

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

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

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

在 [372] 中:ts

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

在 [373]: ts.hour = 0 # <-- 这就是我想要做的。

回答by twasbrillig

Instead of using datetime.datetime, use datetime.dateand it will automatically truncate the hour/minute/second for you.

而不是使用datetime.datetime,使用datetime.date它会自动为您截断小时/分钟/秒。

See https://docs.python.org/library/datetime.html#date-objects

https://docs.python.org/library/datetime.html#date-objects

回答by joris

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

我认为您正在寻找该replace方法(请参阅文档):

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

This is a method inherited from datetime.datetime

这是一个继承自的方法 datetime.datetime

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

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

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

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

还有一种DatetimeIndex.normalize方法,但这在单个时间戳上不可用(我为此打开了一个问题:https: //github.com/pydata/pandas/issues/8794):

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

回答by Andy Jones

pd.Timestamp('2014-11-12 13:35') - pd.offsets.Micro(0, normalize=True) == Timestamp('2014-11-12 00:00:00')

回答by Nadav Aharoni

Note that the replacemethod does not change the Timestamp, so if you want to keep the modified Timestamp you have to assign:

请注意,该replace方法不会更改时间戳,因此如果要保留修改后的时间戳,则必须分配:

In [2]: ts = pd.Timestamp('2014/11/12 13:35')
In [3]: ts.replace(hour=0)
Out[3]: Timestamp('2014-11-12 00:35:00')
In [4]: ts
Out[4]: Timestamp('2014-11-12 13:35:00')

Note: ts is not modified in the code above.

注意:上面代码中没有修改ts。

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