这是一直移动到月底的代码:
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
结果是:
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]')
但是如何将所有时间移动到小时,一天或一周的结束?