Repository: pandas-dev/pandas

Issue:
date_range fails when I try to generate ones with 1 periods and freq equal WOM-1MON 
#### Code Sample

```python
import pandas as pd
pd.date_range('20100104', periods=2, freq='WOM-1MON')  # works
pd.date_range('20100104', periods=1, freq='WOM-1MON')  # fails
```

```python-traceback
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

```python
Out[33]: DatetimeIndex(['2010-01-04'], dtype='datetime64[ns]', freq='WOM-1MON')
```

#### Output of ``pd.show_versions()``

<details>

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


</details>



Discussion:
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`)

https://github.com/pandas-dev/pandas/blob/c4b4a81f56205082ec7f12bf77766e3b74d27c37/pandas/tseries/offsets.py#L1464

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?
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.
@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
Ok, I would like to try. 

Resolving patch:
diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py
--- a/pandas/tseries/offsets.py
+++ b/pandas/tseries/offsets.py
@@ -1461,9 +1461,6 @@ def __init__(self, n=1, normalize=False, week=0, weekday=0):
         self.weekday = weekday
         self.week = week
 
-        if self.n == 0:
-            raise ValueError('N cannot be 0')
-
         if self.weekday < 0 or self.weekday > 6:
             raise ValueError('Day must be 0<=day<=6, got {day}'
                              .format(day=self.weekday))


Test patch:
diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py
--- a/pandas/tests/indexes/datetimes/test_date_range.py
+++ b/pandas/tests/indexes/datetimes/test_date_range.py
@@ -236,6 +236,12 @@ def test_catch_infinite_loop(self):
         pytest.raises(Exception, date_range, datetime(2011, 11, 11),
                       datetime(2011, 11, 12), freq=offset)
 
+    @pytest.mark.parametrize('periods', (1, 2))
+    def test_wom_len(self, periods):
+        # https://github.com/pandas-dev/pandas/issues/20517
+        res = date_range(start='20110101', periods=periods, freq='WOM-1MON')
+        assert len(res) == periods
+
 
 class TestGenRangeGeneration(object):
 
diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py
--- a/pandas/tests/tseries/offsets/test_offsets.py
+++ b/pandas/tests/tseries/offsets/test_offsets.py
@@ -2228,8 +2228,6 @@ class TestWeekOfMonth(Base):
     _offset = WeekOfMonth
 
     def test_constructor(self):
-        tm.assert_raises_regex(ValueError, "^N cannot be 0",
-                               WeekOfMonth, n=0, week=1, weekday=1)
         tm.assert_raises_regex(ValueError, "^Week", WeekOfMonth,
                                n=1, week=4, weekday=0)
         tm.assert_raises_regex(ValueError, "^Week", WeekOfMonth,
@@ -2261,6 +2259,19 @@ def test_offset(self):
             (-1, 2, 1, date3, datetime(2010, 12, 21)),
             (-1, 2, 1, date4, datetime(2011, 1, 18)),
 
+            (0, 0, 1, date1, datetime(2011, 1, 4)),
+            (0, 0, 1, date2, datetime(2011, 2, 1)),
+            (0, 0, 1, date3, datetime(2011, 2, 1)),
+            (0, 0, 1, date4, datetime(2011, 2, 1)),
+            (0, 1, 1, date1, datetime(2011, 1, 11)),
+            (0, 1, 1, date2, datetime(2011, 1, 11)),
+            (0, 1, 1, date3, datetime(2011, 2, 8)),
+            (0, 1, 1, date4, datetime(2011, 2, 8)),
+            (0, 0, 1, date1, datetime(2011, 1, 4)),
+            (0, 1, 1, date2, datetime(2011, 1, 11)),
+            (0, 2, 1, date3, datetime(2011, 1, 18)),
+            (0, 3, 1, date4, datetime(2011, 1, 25)),
+
             (1, 0, 0, date1, datetime(2011, 2, 7)),
             (1, 0, 0, date2, datetime(2011, 2, 7)),
             (1, 0, 0, date3, datetime(2011, 2, 7)),