pandas.DatetimeIndex.snap#

DatetimeIndex.snap(freq='S')[source]#

Snap time stamps to nearest occurring frequency.

Parameters:
freqstr, Timedelta, datetime.timedelta, or DateOffset, default ‘S’

Frequency strings can have multiples, e.g. ‘5h’. See here for a list of frequency aliases.

Returns:
DatetimeIndex

Time stamps to nearest occurring freq.

See also

DatetimeIndex.round

Perform round operation on the data to the specified freq.

DatetimeIndex.floor

Perform floor operation on the data to the specified freq.

Examples

>>> idx = pd.DatetimeIndex(
...     ["2023-01-01", "2023-01-02", "2023-02-01", "2023-02-02"]
... )
>>> idx
DatetimeIndex(['2023-01-01', '2023-01-02', '2023-02-01', '2023-02-02'],
dtype='datetime64[ns]', freq=None)
>>> idx.snap("MS")
DatetimeIndex(['2023-01-01', '2023-01-01', '2023-02-01', '2023-02-01'],
dtype='datetime64[ns]', freq=None)