pandas.Series.dt.as_unit#
- Series.dt.as_unit(unit, round_ok=True)[source]#
Convert to a dtype with the given unit resolution.
The limits of timestamp representation depend on the chosen resolution. Different resolutions can be converted to each other through as_unit.
- Parameters:
- unit{‘s’, ‘ms’, ‘us’, ‘ns’}
- round_okbool, default True
If False and the conversion requires rounding, raise ValueError.
- Returns:
- same type as self
Converted to the specified unit.
See also
Timestamp.as_unitConvert to the given unit.
Examples
For datetime values:
>>> ser = pd.Series(pd.to_datetime(["2020-01-02 01:02:03.004005006"])) >>> ser 0 2020-01-02 01:02:03.004005006 dtype: datetime64[ns] >>> ser.dt.as_unit("s") 0 2020-01-02 01:02:03 dtype: datetime64[s]
For timedelta values:
>>> tdelta_ser = pd.Series(pd.to_timedelta(["1 day 3 min 2 us 42 ns"])) >>> tdelta_ser 0 1 days 00:03:00.000002042 dtype: timedelta64[ns] >>> tdelta_ser.dt.as_unit("s") 0 1 days 00:03:00 dtype: timedelta64[s]