-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
import pandas as pd
# DateIndex containing only two values -- too few to infer a frequency from
too_short = pd.date_range(datetime(2021, 6, 1, 0), datetime(2021, 6, 1, 1), freq="1H")
# raises ValueError, as expected
pd.infer_freq(not_too_short, warn=False)
# also raises ValueError, not as expected
pd.infer_freq(not_too_short, warn=True) # (this is the default arg for warn)
Problem description
The documentation currently suggests that, by default, pd.infer_freq()
will warn when it can't infer a frequency (in this case because the range does not have at least three values). Instead it always raises, regardless of whether the warn
parameter is set to True
or False
.
Looking at the implementation of pd.infer_freq()
, the class that "does the work" is _FrequencyInferer
. This class sets self.warn
in its __init__()
but then never references it again. So it looks like the warn
parameter is a dud.
Expected Output
I'd expect this line to warn instead of raising a ValueError
:
pd.infer_freq(not_too_short, warn=True) # (this is the default arg for warn)
Since lots of code probably implicitly relies on the current implementation of always raising, it may be best to just remove the warn
argument altogether, or document that it doesn't do anything.
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit : 2cb96529396d93b46abab7bbc73a208e708c642e
python : 3.9.1.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.2.4
numpy : 1.20.0
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 49.2.1
Cython : 0.29.22
pytest : 6.2.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : None
gcsfs : 0.7.2
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.17.0
xlrd : None
xlwt : None
numba : None
Comment From: mroeschke
warn was deprecated and will be removed in 2.0 so closing