如何重置大熊猫时间戳的时间部分?
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 时间戳的时间部分
发布评论