pandas.DatetimeIndex.floor — pandas 3.0.0.dev0+2100.gf496acffcc documentation (original) (raw)

DatetimeIndex.floor(freq, ambiguous='raise', nonexistent='raise')[source]#

Perform floor operation on the data to the specified freq.

Parameters:

freqstr or Offset

The frequency level to floor the index to. Must be a fixed frequency like ‘s’ (second) not ‘ME’ (month end). Seefrequency aliases for a list of possible freq values.

ambiguous‘infer’, bool-ndarray, ‘NaT’, default ‘raise’

Only relevant for DatetimeIndex:

nonexistent‘shift_forward’, ‘shift_backward’, ‘NaT’, timedelta, default ‘raise’

A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST.

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 the freq cannot be converted.

See also

DatetimeIndex.floor

Perform floor operation on the data to the specified freq.

DatetimeIndex.snap

Snap time stamps to nearest occurring frequency.

Notes

If the timestamps have a timezone, flooring will take place relative to the local (“wall”) time and re-localized to the same timezone. When flooring near daylight savings time, use nonexistent and ambiguous to control the re-localization behavior.

Examples

DatetimeIndex

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='min') rng.floor('h') DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], dtype='datetime64[ns]', freq=None)

Series

pd.Series(rng).dt.floor("h") 0 2018-01-01 11:00:00 1 2018-01-01 12:00:00 2 2018-01-01 12:00:00 dtype: datetime64[ns]

When rounding near a daylight savings time transition, use ambiguous ornonexistent to 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[s, Europe/Amsterdam]', freq=None)

rng_tz.floor("2h", ambiguous=True) DatetimeIndex(['2021-10-31 02:00:00+02:00'], dtype='datetime64[s, Europe/Amsterdam]', freq=None)