更新时间:2021-12-26 01:25:43
在pandas 0.18.0及更高版本中,日期时间为 ceil
和
In pandas 0.18.0 and later, there are datetime floor
, ceil
and round
methods to round timestamps to a given fixed precision/frequency. To round down to hour precision, you can use:
>>> df['dt2'] = df['dt'].dt.floor('h')
>>> df
dt dt2
0 2014-10-01 10:02:45 2014-10-01 10:00:00
1 2014-10-01 13:08:17 2014-10-01 13:00:00
2 2014-10-01 17:39:24 2014-10-01 17:00:00
这是截断时间戳的另一种方法.与floor
不同,它支持截断到年或月之类的精度.
Here's another alternative to truncate the timestamps. Unlike floor
, it supports truncating to a precision such as year or month.
您可以临时调整基础NumPy datetime64
数据类型的精度单位,将其从[ns]
更改为[h]
:
You can temporarily adjust the precision unit of the underlying NumPy datetime64
datatype, changing it from [ns]
to [h]
:
df['dt'].values.astype('<M8[h]')
这会将所有内容截断为小时精度.例如:
This truncates everything to hour precision. For example:
>>> df
dt
0 2014-10-01 10:02:45
1 2014-10-01 13:08:17
2 2014-10-01 17:39:24
>>> df['dt2'] = df['dt'].values.astype('<M8[h]')
>>> df
dt dt2
0 2014-10-01 10:02:45 2014-10-01 10:00:00
1 2014-10-01 13:08:17 2014-10-01 13:00:00
2 2014-10-01 17:39:24 2014-10-01 17:00:00
>>> df.dtypes
dt datetime64[ns]
dt2 datetime64[ns]
对于其他任何单位,同样的方法也应适用:月份'M'
,分钟'm'
,依此类推:
The same method should work for any other unit: months 'M'
, minutes 'm'
, and so on:
'<M8[Y]'
'<M8[M]'
'<M8[D]'
'<M8[m]'
'<M8[s]'
'<M8[Y]'
'<M8[M]'
'<M8[D]'
'<M8[m]'
'<M8[s]'