Skip to content

Commit 8713abb

Browse files
committed
gfyoung review
Clean error message, check for error in tests, fix typo in whatsnew.
1 parent 63f6b4b commit 8713abb

File tree

10 files changed

+67
-42
lines changed

10 files changed

+67
-42
lines changed

doc/source/whatsnew/v0.21.0.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Previously, :func:`to_datetime` did not localize datetime ``Series`` data when `
310310

311311
Additionally, DataFrames with datetime columns that were parsed by :func:`read_sql_table` and :func:`read_sql_query` will also be localized to UTC only if the original SQL columns were timezone aware datetime columns.
312312

313-
.. _whatsnew_0200.api.consistency_of_range_functions:
313+
.. _whatsnew_0210.api.consistency_of_range_functions:
314314

315315
Consistency of Range Functions
316316
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -338,11 +338,11 @@ New Behavior:
338338

339339
In [2]: pd.interval_range(start=0, end=4, periods=6)
340340
---------------------------------------------------------------------------
341-
ValueError: Must specify exactly two of start, end, or periods
341+
ValueError: Of the three parameters, start, end, and periods, exactly two must be specified
342342

343343
In [3]: pd.period_range(start='2017Q1', end='2017Q4', periods=6, freq='Q')
344344
---------------------------------------------------------------------------
345-
ValueError: Must specify exactly two of start, end, or periods
345+
ValueError: Of the three parameters, start, end, and periods, exactly two must be specified
346346

347347
Additionally, the endpoint parameter ``end`` was not included in the intervals produced by ``interval_range``. However, all other range functions include ``end`` in their output. To promote consistency among the range functions, ``interval_range`` will now include ``end`` as the right endpoint of the final interval, except if ``freq`` is specified in a way which skips ``end``.
348348

pandas/core/indexes/datetimes.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ def __new__(cls, data=None,
412412
def _generate(cls, start, end, periods, name, offset,
413413
tz=None, normalize=False, ambiguous='raise', closed=None):
414414
if com._count_not_none(start, end, periods) != 2:
415-
msg = 'Must specify exactly two of start, end, or periods'
416-
raise ValueError(msg)
415+
raise ValueError('Of the three parameters, start, end, and '
416+
'periods, exactly two must be specified')
417417

418418
_normalized = True
419419

@@ -2031,7 +2031,8 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None,
20312031
20322032
Notes
20332033
-----
2034-
Exactly two of start, end, or periods must be specified
2034+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
2035+
must be specified.
20352036
20362037
To learn more about the frequency strings, please see `this link
20372038
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
@@ -2074,7 +2075,8 @@ def bdate_range(start=None, end=None, periods=None, freq='B', tz=None,
20742075
20752076
Notes
20762077
-----
2077-
Exactly two of start, end, or periods must be specified
2078+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
2079+
must be specified.
20782080
20792081
To learn more about the frequency strings, please see `this link
20802082
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
@@ -2128,7 +2130,8 @@ def cdate_range(start=None, end=None, periods=None, freq='C', tz=None,
21282130
21292131
Notes
21302132
-----
2131-
Exactly two of start, end, or periods must be specified
2133+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
2134+
must be specified.
21322135
21332136
To learn more about the frequency strings, please see `this link
21342137
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.

pandas/core/indexes/interval.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1048,14 +1048,16 @@ def interval_range(start=None, end=None, freq=None, periods=None,
10481048
10491049
Notes
10501050
-----
1051-
Exactly two of start, end, or periods must be specified
1051+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
1052+
must be specified.
10521053
10531054
Returns
10541055
-------
10551056
rng : IntervalIndex
10561057
"""
10571058
if com._count_not_none(start, end, periods) != 2:
1058-
raise ValueError('Must specify exactly two of start, end, or periods')
1059+
raise ValueError('Of the three parameters, start, end, and periods, '
1060+
'exactly two must be specified')
10591061

10601062
if freq is None:
10611063
freq = 1

pandas/core/indexes/period.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,8 @@ def tz_localize(self, tz, infer_dst=False):
10521052

10531053
def _get_ordinal_range(start, end, periods, freq, mult=1):
10541054
if com._count_not_none(start, end, periods) != 2:
1055-
raise ValueError('Must specify exactly two of start, end, or periods')
1055+
raise ValueError('Of the three parameters, start, end, and periods, '
1056+
'exactly two must be specified')
10561057

10571058
if freq is not None:
10581059
_, mult = _gfc(freq)
@@ -1173,7 +1174,8 @@ def period_range(start=None, end=None, periods=None, freq='D', name=None):
11731174
11741175
Notes
11751176
-----
1176-
Exactly two of start, end, or periods must be specified
1177+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
1178+
must be specified.
11771179
11781180
To learn more about the frequency strings, please see `this link
11791181
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
@@ -1182,5 +1184,9 @@ def period_range(start=None, end=None, periods=None, freq='D', name=None):
11821184
-------
11831185
prng : PeriodIndex
11841186
"""
1187+
if com._count_not_none(start, end, periods) != 2:
1188+
raise ValueError('Of the three parameters, start, end, and periods, '
1189+
'exactly two must be specified')
1190+
11851191
return PeriodIndex(start=start, end=end, periods=periods,
11861192
freq=freq, name=name)

pandas/core/indexes/timedeltas.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ def __new__(cls, data=None, unit=None,
234234
@classmethod
235235
def _generate(cls, start, end, periods, name, offset, closed=None):
236236
if com._count_not_none(start, end, periods) != 2:
237-
msg = 'Must specify exactly two of start, end, or periods'
238-
raise ValueError(msg)
237+
raise ValueError('Of the three parameters, start, end, and '
238+
'periods, exactly two must be specified')
239239

240240
if start is not None:
241241
start = Timedelta(start)
@@ -986,7 +986,8 @@ def timedelta_range(start=None, end=None, periods=None, freq='D',
986986
987987
Notes
988988
-----
989-
Exactly two of start, end, or periods must be specified.
989+
Of the three parameters, ``start``, ``end``, and ``periods``, exactly two
990+
must be specified.
990991
991992
To learn more about the frequency strings, please see `this link
992993
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.

pandas/tests/indexes/datetimes/test_date_range.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def test_date_range_ambiguous_arguments(self):
107107
start = datetime(2011, 1, 1, 5, 3, 40)
108108
end = datetime(2011, 1, 1, 8, 9, 40)
109109

110-
with pytest.raises(ValueError):
110+
msg = ('Of the three parameters, start, end, and periods, '
111+
'exactly two must be specified')
112+
with tm.assert_raises_regex(ValueError, msg):
111113
date_range(start, end, periods=10, freq='s')
112114

113115
def test_date_range_businesshour(self):
@@ -146,25 +148,28 @@ def test_date_range_businesshour(self):
146148

147149
def test_range_misspecified(self):
148150
# GH #1095
149-
with pytest.raises(ValueError):
151+
msg = ('Of the three parameters, start, end, and periods, '
152+
'exactly two must be specified')
153+
154+
with tm.assert_raises_regex(ValueError, msg):
150155
date_range(start='1/1/2000')
151156

152-
with pytest.raises(ValueError):
157+
with tm.assert_raises_regex(ValueError, msg):
153158
date_range(end='1/1/2000')
154159

155-
with pytest.raises(ValueError):
160+
with tm.assert_raises_regex(ValueError, msg):
156161
date_range(periods=10)
157162

158-
with pytest.raises(ValueError):
163+
with tm.assert_raises_regex(ValueError, msg):
159164
date_range(start='1/1/2000', freq='H')
160165

161-
with pytest.raises(ValueError):
166+
with tm.assert_raises_regex(ValueError, msg):
162167
date_range(end='1/1/2000', freq='H')
163168

164-
with pytest.raises(ValueError):
169+
with tm.assert_raises_regex(ValueError, msg):
165170
date_range(periods=10, freq='H')
166171

167-
with pytest.raises(ValueError):
172+
with tm.assert_raises_regex(ValueError, msg):
168173
date_range()
169174

170175
def test_compat_replace(self):

pandas/tests/indexes/period/test_construction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ def test_constructor_error(self):
440440
with tm.assert_raises_regex(ValueError, msg):
441441
PeriodIndex(start=start, end=end_intv)
442442

443-
msg = 'Must specify exactly two of start, end, or periods'
443+
msg = ('Of the three parameters, start, end, and periods, '
444+
'exactly two must be specified')
444445
with tm.assert_raises_regex(ValueError, msg):
445446
PeriodIndex(start=start)
446447

pandas/tests/indexes/period/test_period_range.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ def test_construction(self, freq):
3535

3636
def test_errors(self):
3737
# not enough params
38-
with pytest.raises(ValueError):
38+
msg = ('Of the three parameters, start, end, and periods, '
39+
'exactly two must be specified')
40+
with tm.assert_raises_regex(ValueError, msg):
3941
period_range(start='2017Q1')
4042

41-
with pytest.raises(ValueError):
43+
with tm.assert_raises_regex(ValueError, msg):
4244
period_range(end='2017Q1')
4345

44-
with pytest.raises(ValueError):
46+
with tm.assert_raises_regex(ValueError, msg):
4547
period_range(periods=5)
4648

47-
with pytest.raises(ValueError):
49+
with tm.assert_raises_regex(ValueError, msg):
4850
period_range()
4951

5052
# too many params
51-
with pytest.raises(ValueError):
53+
with tm.assert_raises_regex(ValueError, msg):
5254
period_range(start='2017Q1', end='2018Q1', periods=8, freq='Q')

pandas/tests/indexes/test_interval.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -759,27 +759,31 @@ def test_construction(self, closed):
759759

760760
def test_errors(self):
761761
# not enough params
762-
with pytest.raises(ValueError):
762+
msg = ('Of the three parameters, start, end, and periods, '
763+
'exactly two must be specified')
764+
765+
with tm.assert_raises_regex(ValueError, msg):
763766
interval_range(start=0)
764767

765-
with pytest.raises(ValueError):
768+
with tm.assert_raises_regex(ValueError, msg):
766769
interval_range(end=5)
767770

768-
with pytest.raises(ValueError):
771+
with tm.assert_raises_regex(ValueError, msg):
769772
interval_range(periods=2)
770773

771-
with pytest.raises(ValueError):
774+
with tm.assert_raises_regex(ValueError, msg):
772775
interval_range()
773776

774777
# too many params
775-
with pytest.raises(ValueError):
778+
with tm.assert_raises_regex(ValueError, msg):
776779
interval_range(start=0, end=5, periods=6)
777780

778781
# mixed units
779-
with pytest.raises(ValueError):
782+
msg = 'start, end, freq need to be the same type'
783+
with tm.assert_raises_regex(ValueError, msg):
780784
interval_range(start=0, end=Timestamp('20130101'), freq=2)
781785

782-
with pytest.raises(ValueError):
786+
with tm.assert_raises_regex(ValueError, msg):
783787
interval_range(start=0, end=10, freq=Timedelta('1day'))
784788

785789

pandas/tests/indexes/timedeltas/test_timedelta_range.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import pytest
32
import pandas as pd
43
import pandas.util.testing as tm
54
from pandas.tseries.offsets import Day, Second
@@ -52,18 +51,20 @@ def test_timedelta_range(self):
5251

5352
def test_errors(self):
5453
# not enough params
55-
with pytest.raises(ValueError):
54+
msg = ('Of the three parameters, start, end, and periods, '
55+
'exactly two must be specified')
56+
with tm.assert_raises_regex(ValueError, msg):
5657
timedelta_range(start='0 days')
5758

58-
with pytest.raises(ValueError):
59+
with tm.assert_raises_regex(ValueError, msg):
5960
timedelta_range(end='5 days')
6061

61-
with pytest.raises(ValueError):
62+
with tm.assert_raises_regex(ValueError, msg):
6263
timedelta_range(periods=2)
6364

64-
with pytest.raises(ValueError):
65+
with tm.assert_raises_regex(ValueError, msg):
6566
timedelta_range()
6667

6768
# too many params
68-
with pytest.raises(ValueError):
69+
with tm.assert_raises_regex(ValueError, msg):
6970
timedelta_range(start='0 days', end='5 days', periods=10)

0 commit comments

Comments
 (0)