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

date_range fails when I try to generate ones with 1 periods and freq equal WOM-1MON #20517

Closed
mmngreco opened this Issue Mar 28, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@mmngreco
Contributor

mmngreco commented Mar 28, 2018

Code Sample

import pandas as pd
pd.date_range('20100104', periods=2, freq='WOM-1MON')  # works
pd.date_range('20100104', periods=1, freq='WOM-1MON')  # fails
Traceback (most recent call last):
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-31-ec0b4bad59c9>", line 1, in <module>
    pd.date_range('20100104', periods=1, freq='WOM-1MON')
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py", line 2057, in date_range
    closed=closed, **kwargs)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/util/_decorators.py", line 118, in wrapper
    return func(*args, **kwargs)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py", line 324, in __new__
    ambiguous=ambiguous)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py", line 531, in _generate
    index = _generate_regular_range(start, end, periods, offset)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py", line 2009, in _generate_regular_range
    dates = list(xdr)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 2960, in generate_range
    end = start + (periods - 1) * offset
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 425, in __rmul__
    return self.__mul__(someInt)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 422, in __mul__
    **self.kwds)
  File "/Users/mmngreco/miniconda3/envs/pandas-dev/lib/python3.6/site-packages/pandas/tseries/offsets.py", line 1671, in __init__
    raise ValueError('N cannot be 0')
ValueError: N cannot be 0

Problem description

If N is equal to periods then is not 0 as we can see, that make me think that probably there is something wrong in the code.

Expected Output

Out[33]: DatetimeIndex(['2010-01-04'], dtype='datetime64[ns]', freq='WOM-1MON')

Output of pd.show_versions()

pd.show_versions()
INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: es_ES.UTF-8
pandas: 0.22.0
pytest: 3.4.2
pip: 9.0.1
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: 0.9.0
xarray: 0.10.2
IPython: 6.2.1
sphinx: 1.7.1
patsy: 0.5.0
dateutil: 2.7.0
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.2.2
openpyxl: 2.5.1
xlrd: 1.1.0
xlwt: 1.2.0
xlsxwriter: 1.0.2
lxml: 4.2.0
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: None
jinja2: 2.10
s3fs: 0.1.3
fastparquet: 0.1.4
pandas_gbq: None
pandas_datareader: None

@WillAyd

This comment has been minimized.

Member

WillAyd commented Mar 30, 2018

Well the traceback here points exactly to the offending line of code - within the constructor there is an explicit check that you have more than one period which I've linked below for reference (generate_periods up one level in the stack subtracts one from periods)

if self.n == 0:

If you remove that assertion you get the value you are expecting. I'm not overly familiar with offsets though - @jbrockmendel any chance you know whether it makes sense to relax that assertion or not?

@jbrockmendel

This comment has been minimized.

Member

jbrockmendel commented Mar 30, 2018

This is one of those "it was like that when I got here" things. I'd guess that n==0 would cause trouble with incrementing, but not really sure.

@WillAyd

This comment has been minimized.

Member

WillAyd commented Mar 30, 2018

@mmngreco do you want to try a PR for this? Would need test cases to cover this and any other edge case you can think of

@mmngreco

This comment has been minimized.

Contributor

mmngreco commented Mar 30, 2018

Ok, I would like to try.

mmngreco added a commit to mmngreco/pandas that referenced this issue Mar 30, 2018

mmngreco added a commit to mmngreco/pandas that referenced this issue Mar 30, 2018

@mmngreco mmngreco referenced this issue Mar 30, 2018

Merged

Fixed WOM offset when n=0 #20549

4 of 4 tasks complete

@jreback jreback added this to the 0.23.0 milestone Mar 30, 2018

@tv3141

This comment has been minimized.

Contributor

tv3141 commented Mar 30, 2018

The same check can be found for three other offsets:

import pandas as pd
from pandas.tseries.offsets import *

# all three lines below return ValueError: N cannot be 0
pd.date_range('20100104', periods=1, freq=LastWeekOfMonth(weekday=2))
pd.date_range('20100104', periods=1, freq=FY5253(startingMonth=4, weekday=2, variation='nearest'))
pd.date_range('20100104', periods=1, freq=FY5253Quarter(qtr_with_extra_week=1, startingMonth=4, weekday=2, variation='nearest'))

LastWeekOfMonth

if self.n == 0:
raise ValueError('N cannot be 0')

FY5253

if self.n == 0:
raise ValueError('N cannot be 0')

FY5253Quarter

if self.n == 0:
raise ValueError('N cannot be 0')

mmngreco added a commit to mmngreco/pandas that referenced this issue Mar 30, 2018

@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018

mmngreco added a commit to mmngreco/pandas that referenced this issue Apr 20, 2018

mmngreco added a commit to mmngreco/pandas that referenced this issue Apr 20, 2018

mmngreco added a commit to mmngreco/pandas that referenced this issue Apr 20, 2018

@jreback jreback modified the milestones: Next Major Release, 0.23.0 Apr 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment