- API reference
- Series
- pandas.Serie...
pandas.Series.dt.round#
- Series.dt.round(*args,**kwargs)[source]#
Perform round operation on the data to the specifiedfreq.
- Parameters:
- freqstr or Offset
The frequency level to round the index to. Must be a fixedfrequency like ‘S’ (second) not ‘ME’ (month end). Seefrequency aliases fora list of possiblefreq values.
- ambiguous‘infer’, bool-ndarray, ‘NaT’, default ‘raise’
Only relevant for DatetimeIndex:
‘infer’ will attempt to infer fall dst-transition hours based onorder
bool-ndarray where True signifies a DST time, False designatesa non-DST time (note that this flag is only applicable forambiguous times)
‘NaT’ will return NaT where there are ambiguous times
‘raise’ will raise an AmbiguousTimeError if there are ambiguoustimes.
- nonexistent‘shift_forward’, ‘shift_backward’, ‘NaT’, timedelta, default ‘raise’
A nonexistent time does not exist in a particular timezonewhere clocks moved forward due to DST.
‘shift_forward’ will shift the nonexistent time forward to theclosest existing time
‘shift_backward’ will shift the nonexistent time backward to theclosest existing time
‘NaT’ will return NaT where there are nonexistent times
timedelta objects will shift nonexistent times by the timedelta
‘raise’ will raise an NonExistentTimeError if there arenonexistent times.
- Returns:
- DatetimeIndex, TimedeltaIndex, or Series
Index of the same type for a DatetimeIndex or TimedeltaIndex,or a Series with the same index for a Series.
- Raises:
- ValueError if thefreq cannot be converted.
Notes
If the timestamps have a timezone, rounding will take place relative to thelocal (“wall”) time and re-localized to the same timezone. When roundingnear daylight savings time, use
nonexistentandambiguoustocontrol the re-localization behavior.Examples
DatetimeIndex
>>>rng=pd.date_range('1/1/2018 11:59:00',periods=3,freq='min')>>>rngDatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], dtype='datetime64[ns]', freq='min')>>>rng.round('h')DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], dtype='datetime64[ns]', freq=None)
Series
>>>pd.Series(rng).dt.round("h")0 2018-01-01 12:00:001 2018-01-01 12:00:002 2018-01-01 12:00:00dtype: datetime64[ns]
When rounding near a daylight savings time transition, use
ambiguousornonexistentto control how the timestamp should be re-localized.>>>rng_tz=pd.DatetimeIndex(["2021-10-31 03:30:00"],tz="Europe/Amsterdam")
>>>rng_tz.floor("2h",ambiguous=False)DatetimeIndex(['2021-10-31 02:00:00+01:00'], dtype='datetime64[ns, Europe/Amsterdam]', freq=None)
>>>rng_tz.floor("2h",ambiguous=True)DatetimeIndex(['2021-10-31 02:00:00+02:00'], dtype='datetime64[ns, Europe/Amsterdam]', freq=None)