Skip to content

Commit 5473bf1

Browse files
committed
Try to match behavior
1 parent 959452c commit 5473bf1

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

pandas/core/indexes/period.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,31 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
194194

195195
if data is None and ordinal is None:
196196
# range-based.
197-
data, freq = PeriodArray._generate_range(start, end, periods,
198-
freq, fields)
197+
data, freq2 = PeriodArray._generate_range(start, end, periods,
198+
freq, fields)
199199
# PeriodArray._generate range does validate that fields is
200200
# empty when really using the range-based constructor.
201201
if not fields:
202-
warnings.warn("Creating a PeriodIndex by passing range "
203-
"endpoints is deprecated. Use "
204-
"`pandas.period_range` instead.",
205-
FutureWarning, stacklevel=2)
202+
msg = ("Creating a PeriodIndex by passing range "
203+
"endpoints is deprecated. Use "
204+
"`pandas.period_range` instead.")
205+
# period_range differs from PeriodIndex for cases like
206+
# start="2000", periods=4
207+
# PeriodIndex interprets that as A-DEC freq.
208+
# period_range interprets it as 'D' freq.
209+
cond = (
210+
freq is None and (
211+
(start and not isinstance(start, Period)) or
212+
(end and not isinstance(end, Period))
213+
)
214+
)
215+
if cond:
216+
msg += (
217+
" Note that the default `freq` may differ. Pass "
218+
"'freq=\"{}\"' to ensure the same output."
219+
).format(freq2.freqstr)
220+
warnings.warn(msg, FutureWarning, stacklevel=2)
221+
freq = freq2
206222

207223
data = PeriodArray(data, freq=freq)
208224
else:
@@ -1058,6 +1074,9 @@ def period_range(start=None, end=None, periods=None, freq=None, name=None):
10581074
if com.count_not_none(start, end, periods) != 2:
10591075
raise ValueError('Of the three parameters: start, end, and periods, '
10601076
'exactly two must be specified')
1077+
if freq is None and (not isinstance(start, Period)
1078+
and not isinstance(end, Period)):
1079+
freq = 'D'
10611080

10621081
data, freq = PeriodArray._generate_range(start, end, periods, freq,
10631082
fields={})

pandas/tests/indexes/period/test_construction.py

+7
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ def test_constructor_range_based_deprecated(self):
388388
pi = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
389389
assert len(pi) == 9
390390

391+
def test_constructor_range_based_deprecated_different_freq(self):
392+
with tm.assert_produces_warning(FutureWarning) as m:
393+
PeriodIndex(start='2000', periods=2)
394+
395+
warning, = m
396+
assert 'freq="A-DEC"' in str(warning.message)
397+
391398
def test_constructor(self):
392399
pi = period_range(freq='A', start='1/1/2001', end='12/1/2009')
393400
assert len(pi) == 9

0 commit comments

Comments
 (0)