maxframe.dataframe.Series.dt.round#
- Series.dt.round(freq, ambiguous: Literal['infer', 'NaT', 'raise'] | bool | ndarray[Any, dtype[bool_]] = 'raise', nonexistent: Literal['shift_forward', 'shift_backward', 'NaT', 'raise'] | timedelta = 'raise') Self#
对数据执行指定 freq 的四舍五入操作。
- 参数:
freq (str or Offset) -- The frequency level to round the index to. Must be a fixed frequency like 's' (second) not 'ME' (month end). See frequency aliases for a list of possible freq values.
ambiguous ('infer', bool-ndarray, 'NaT', default 'raise') --
仅对 DatetimeIndex 有效:
'infer' 将根据顺序尝试推断夏令时转换时间
bool-ndarray,其中 True 表示夏令时,False 表示非夏令时(注意此标志仅适用于时间歧义)
'NaT' 在时间歧义处返回 NaT
'raise' will raise a ValueError if there are ambiguous times.
nonexistent ('shift_forward', 'shift_backward', 'NaT', timedelta, default 'raise') --
不存在的时间是指在特定时区中由于夏令时导致时钟向前移动而不存在的时间。
'shift_forward' 将不存在的时间向前移动到最近的现有时间
'shift_backward' 将不存在的时间向后移动到最近的现有时间
'NaT' 在不存在时间处返回 NaT
timedelta 对象将按 timedelta 移位不存在的时间
'raise' will raise a ValueError if there are nonexistent times.
- 返回:
对于 DatetimeIndex 或 TimedeltaIndex 返回相同类型的索引,对于 Series 返回具有相同索引的 Series。
- 返回类型:
DatetimeIndex, TimedeltaIndex, or Series
- 抛出:
ValueError if the freq cannot be converted. --
参见
DatetimeIndex.floorPerform floor operation on the data to the specified freq.
DatetimeIndex.snapSnap time stamps to nearest occurring frequency.
备注
如果时间戳具有时区,则四舍五入将相对于本地(“墙上”)时间进行,并重新本地化到相同时区。在夏令时附近进行四舍五入时,使用
nonexistent和ambiguous来控制重新本地化行为。示例
DatetimeIndex
>>> import maxframe.dataframe as md >>> rng = md.date_range("1/1/2018 11:59:00", periods=3, freq="min") >>> rng.execute() DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], dtype='datetime64[us]', freq='min')
>>> rng.round('h').execute() DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], dtype='datetime64[us]', freq=None)
Series
>>> md.Series(rng).dt.round("h").execute() 0 2018-01-01 12:00:00 1 2018-01-01 12:00:00 2 2018-01-01 12:00:00 dtype: datetime64[us]
在夏令时转换时间附近进行四舍五入时,使用
ambiguous或nonexistent控制时间戳的重新本地化方式。>>> rng_tz = md.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam")
>>> rng_tz.floor("2h", ambiguous=False).execute() DatetimeIndex(['2021-10-31 02:00:00+01:00'], dtype='datetime64[us, Europe/Amsterdam]', freq=None)
>>> rng_tz.floor("2h", ambiguous=True).execute() DatetimeIndex(['2021-10-31 02:00:00+02:00'], dtype='datetime64[us, Europe/Amsterdam]', freq=None)