Pandas | infer_freq method
Start your free 7-days trial now!
Pandas infer_freq(~)
method returns a string indicating the inferred frequency of the given DatetimeIndex
or a TimedeltaIndex
.
Parameters
1. index
link | DatetimeIndex
or TimedeltaIndex
The index from which to infer the frequency.
2. warn
| boolean
| optional
Whether or not to print warning message if the frequency cannot be inferred. Upon testing, this parameter seems to not do anything. By default, warn=True
.
This method can raise the following errors:
a
ValueError
is thrown if the index has fewer than 3 dates.a
TypeError
is thrown if the index is not of type datetime-like.
Return Value
A string indicating the inferred frequency.
Examples
Basic usage
Consider the following DatetimeIndex
:
idx
DatetimeIndex(['2020-12-25', '2020-12-27', '2020-12-29'], dtype='datetime64[ns]', freq=None)
Because we did not explicitly specify the freq
parameter in the constructor, we end up with freq=None
.
To infer the frequency:
pd.infer_freq(idx)
'2D'
This tells us that the inferred frequency (offset) is 2 days.