[英]How to move the datetime to end of day, week or hour
Here is the code that move all the times to the end of month: 以下是将所有时间都移动到月末的代码:
import numpy as np
import pandas as pd
times = np.array([
'2013-07-22T02:10:32.000000000+0900',
'2013-07-22T01:11:13.000000000+0900',
'2013-07-21T23:23:32.000000000+0900',
'2013-07-21T05:59:21.000000000+0900',
'2013-07-21T05:57:30.000000000+0900',
'2013-07-21T05:44:27.000000000+0900',
'2013-07-20T10:45:17.000000000+0900',
'2013-07-20T10:36:53.000000000+0900',
'2013-07-20T09:57:46.000000000+0900',
'2013-07-20T09:57:06.000000000+0900',
'2013-07-20T09:30:57.000000000+0900',
'2013-07-20T08:20:27.000000000+0900',], dtype='datetime64[ns]')
dti = pd.DatetimeIndex(times)
dti.shift(1, "M").values
The result is: 结果是:
array(['2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900',
'2013-07-31T09:00:00.000000000+0900'], dtype='datetime64[ns]')
but how to move all the times to the end of the hour, day or week? 但是如何将所有时间一直移动到小时,一天或一周结束?
The best method i found for this is by to_period
& to_timestamp
: 我找到的最好的方法是
to_period
和to_timestamp
:
In [39]:
dti.to_period("W-SAT").to_timestamp(how="end").values
Out[39]:
array(['2013-07-27T09:00:00.000000000+0900',
'2013-07-27T09:00:00.000000000+0900',
'2013-07-27T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900',
'2013-07-20T09:00:00.000000000+0900'], dtype='datetime64[ns]')
In [40]:
dti.to_period("H").to_timestamp(how="end").values
Out[40]:
array(['2013-07-22T02:59:59.000000000+0900',
'2013-07-22T01:59:59.000000000+0900',
'2013-07-21T23:59:59.000000000+0900',
'2013-07-21T05:59:59.000000000+0900',
'2013-07-21T05:59:59.000000000+0900',
'2013-07-21T05:59:59.000000000+0900',
'2013-07-20T10:59:59.000000000+0900',
'2013-07-20T10:59:59.000000000+0900',
'2013-07-20T09:59:59.000000000+0900',
'2013-07-20T09:59:59.000000000+0900',
'2013-07-20T09:59:59.000000000+0900',
'2013-07-20T08:59:59.000000000+0900'], dtype='datetime64[ns]')
I agree with Andy; 我同意安迪; that can't be the intended behavior of
shift
. 这不可能是
shift
的预期行为。 A cleaner way to shift times to the end of the month is this: 将时间转移到月末的更简洁方法是:
from pandas.tseries.offsets import MonthEnd
times = Series(times)
times.map(lambda x: x + MonthEnd())
But there is no such thing as HourEnd, DayEnd, or WeekEnd. 但是没有HourEnd,DayEnd或WeekEnd这样的东西。 For those cases, how about following this pattern?
对于那些情况,如何遵循这种模式?
from pandas.tseries.offsets import Second, Minute, Hour, Day
times.map(lambda x: x + Minute(59-x.minute) + Second(59-x.second))
times.map(lambda x: x + Hour(23-x.hour) + Minute(59-x.minute) + Second(59-x.second))
times.map(lambda x: x + Day(6-x.weekday()) + Hour(23-x.hour) + \
Minute(59-x.minute) + Second(59-x.second))
If you want the last day of the week but not necessarily the last second of that day, then the expression is obviously simpler. 如果你想在一周的最后一天,但不一定是那一天的最后一秒 ,然后表达显然是简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.