pandas.DatetimeIndex.snap#
- DatetimeIndex.snap(freq='S')[source]#
Snap time stamps to nearest occurring frequency.
Deprecated since version 3.2.0: For fixed frequencies
snapreturns the values unchanged; useDatetimeIndex.round()if rounding was intended. For other offsets, use::offset = pd.tseries.frequencies.to_offset(freq) back, fwd = idx.map(offset.rollback), idx.map(offset.rollforward) fwd.where(fwd - idx <= idx - back, back)
Each timestamp in the index is adjusted to the nearest occurrence of the specified frequency, snapping backward or forward as appropriate.
- 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.roundPerform round operation on the data to the specified freq.
DatetimeIndex.floorPerform 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)