pandas.TimedeltaIndex.set_freq#
- TimedeltaIndex.set_freq(freq)[source]#
Return a new Index with the given frequency.
The values are unchanged; only the
freqattribute differs. Unlike assigning tofreq, this does not modify the original Index.- Parameters:
- freqstr, DateOffset or None
Frequency to set. It must conform to the values of the Index.
"infer"sets the frequency inferred from the values, orNoneif none can be inferred.Noneremoves the frequency.
- Returns:
- DatetimeIndex or TimedeltaIndex
A new Index with
freqset.
- Raises:
- ValueError
If
freqis invalid or does not conform to the values of the Index.- TypeError
If the Index is an empty TimedeltaIndex and
freqis not a fixed frequency.
See also
DatetimeIndex.freqReturn the frequency object if it is set.
DatetimeIndex.inferred_freqReturn the frequency inferred from the values.
Examples
>>> idx = pd.DatetimeIndex(["2020-01-01", "2020-01-03", "2020-01-05"]) >>> idx DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05'], dtype='datetime64[us]', freq=None) >>> idx.set_freq("2D") DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05'], dtype='datetime64[us]', freq='2D') >>> idx.set_freq("2D").set_freq(None) DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05'], dtype='datetime64[us]', freq=None) >>> idx.set_freq("infer") DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05'], dtype='datetime64[us]', freq='2D')