Skip to content

BUG: pd.date_range gives a error when freq = LastWeekOfMonth(1) #41563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
zgao12 opened this issue May 19, 2021 · 0 comments
Open
2 of 3 tasks

BUG: pd.date_range gives a error when freq = LastWeekOfMonth(1) #41563

zgao12 opened this issue May 19, 2021 · 0 comments
Labels
Bug Frequency DateOffsets

Comments

@zgao12
Copy link

zgao12 commented May 19, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> from pandas.tseries.offsets import *
>>> fq = LastWeekOfMonth(1)
>>> pd.date_range('2018-04-01', periods=1, freq=fq)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/miniconda3/envs/test37/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py", line 1077, in date_range
    **kwargs,
  File "/home/miniconda3/envs/test37/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py", line 416, in _generate_range
    values = np.array([x.value for x in xdr], dtype=np.int64)
  File "/home/miniconda3/envs/test37/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py", line 416, in <listcomp>
    values = np.array([x.value for x in xdr], dtype=np.int64)
  File "/home/miniconda3/envs/test37/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py", line 2438, in generate_range
    end = start + (periods - 1) * offset
  File "pandas/_libs/tslibs/offsets.pyx", line 459, in pandas._libs.tslibs.offsets.BaseOffset.__mul__
  File "pandas/_libs/tslibs/offsets.pyx", line 455, in pandas._libs.tslibs.offsets.BaseOffset.__mul__
  File "pandas/_libs/tslibs/offsets.pyx", line 2647, in pandas._libs.tslibs.offsets.LastWeekOfMonth.__init__
ValueError: N cannot be 0

Problem description

The output of date_range with period = 1, freq = LastWeekOfMonth should not raise a error, since other freq like WeekOfMonth gives a normal output with period = 1.

Expected Output

DatetimeIndex(['2018-04-30'], dtype='datetime64[ns]', freq='LWOM-MON')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.7.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-72-generic
Version : #80~18.04.1-Ubuntu SMP Mon Apr 12 23:26:25 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.2.4
numpy : 1.19.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : None
pytest : None
hypothesis : None
sphinx : 3.5.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@zgao12 zgao12 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 19, 2021
@zgao12 zgao12 changed the title BUG: BUG: pd.date_range gives a error when freq = LastWeekOfMonth(1) May 20, 2021
@mroeschke mroeschke added Frequency DateOffsets and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets
Projects
None yet
Development

No branches or pull requests

2 participants