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日"],
...  dtype="M8[ns]",
... )
>>> 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)
On this page

This Page