pandas.TimedeltaIndex.as_unit#

TimedeltaIndex.as_unit(unit)[源代码]#

转换为具有给定单位分辨率的 dtype。

参数:
单元{‘s’, ‘ms’, ‘us’, ‘ns’}
返回:
与自身类型相同

例子

对于 pandas.DatetimeIndex:

>>> idx = pd.DatetimeIndex(["2020-01-02 01:02:03.004005006"])
>>> idx
DatetimeIndex(['2020-01-02 01:02:03.004005006'],
              dtype='datetime64[ns]', freq=None)
>>> idx.as_unit("s")
DatetimeIndex(['2020-01-02 01:02:03'], dtype='datetime64[s]', freq=None)

对于 pandas.TimedeltaIndex:

>>> tdelta_idx = pd.to_timedelta(["1 day 3 min 2 us 42 ns"])
>>> tdelta_idx
TimedeltaIndex(['1 days 00:03:00.000002042'],
                dtype='timedelta64[ns]', freq=None)
>>> tdelta_idx.as_unit("s")
TimedeltaIndex(['1 days 00:03:00'], dtype='timedelta64[s]', freq=None)