当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pandas.Series.dt.ceil用法及代码示例


用法:

Series.dt.ceil(*args, **kwargs)

对指定freq的数据进行ceil操作。

参数

freqstr 或偏移量

索引上限的频率级别。必须是固定频率,例如“S”(秒)而不是“ME”(月末)。有关可能的 freq 值的列表,请参阅频率别名。

ambiguous‘infer’, bool-ndarray, ‘NaT’, 默认 ‘raise’

仅与 DatetimeIndex 相关:

  • ‘infer’ 将尝试根据订单推断秋季 dst-transition 小时

  • bool-ndarray 其中 True 表示 DST 时间,False 表示非 DST 时间(请注意,此标志仅适用于模棱两可的时间)

  • 'NaT' 将在时间不明确的地方返回 NaT

  • 如果时间不明确,‘raise’ 将引发 AmbiguousTimeError。

nonexistent‘shift_forward’, ‘shift_backward’, ‘NaT’, timedelta, 默认 ‘raise’

由于 DST,时钟向前移动的特定时区不存在不存在的时间。

  • ‘shift_forward’ 将不存在的时间向前移动到最接近的存在时间

  • ‘shift_backward’ 将不存在的时间向后移动到最接近的存在时间

  • ‘NaT’ 将返回不存在时间的 NaT

  • timedelta 对象会将不存在的时间移动 timedelta

  • 如果时间不存在,‘raise’ 将引发 NonExistentTimeError。

返回

DatetimeIndex、TimedeltaIndex 或 Series

DatetimeIndex 或 TimedeltaIndex 具有相同类型的索引,或者 Series 具有相同索引的 Series。

抛出

如果无法转换 freq,则出现 ValueError。

注意

如果时间戳有一个时区,上限将相对于本地 (“wall”) 时间发生并重新本地化到同一时区。当上限接近夏令时时,使用nonexistentambiguous 来控制重新定位行为。

例子

日期时间索引

>>> rng = pd.date_range('1/1/2018 11:59:00', periods=3, freq='min')
>>> rng
DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00',
               '2018-01-01 12:01:00'],
              dtype='datetime64[ns]', freq='T')
>>> rng.ceil('H')
DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00',
               '2018-01-01 13:00:00'],
              dtype='datetime64[ns]', freq=None)

Series

>>> pd.Series(rng).dt.ceil("H")
0   2018-01-01 12:00:00
1   2018-01-01 12:00:00
2   2018-01-01 13:00:00
dtype:datetime64[ns]

在夏令时转换附近四舍五入时,使用ambiguousnonexistent 来控制如何重新定位时间戳。

>>> rng_tz = pd.DatetimeIndex(["2021-10-31 01:30:00"], tz="Europe/Amsterdam")
>>> rng_tz.ceil("H", ambiguous=False)
DatetimeIndex(['2021-10-31 02:00:00+01:00'],
              dtype='datetime64[ns, Europe/Amsterdam]', freq=None)
>>> rng_tz.ceil("H", ambiguous=True)
DatetimeIndex(['2021-10-31 02:00:00+02:00'],
              dtype='datetime64[ns, Europe/Amsterdam]', freq=None)

相关用法


注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Series.dt.ceil。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。