Skip to content

Commit 8a814a0

Browse files
authored
ENH: Improve error message when constructing period with invalid freq (#55940)
improve error message when construct period with invalid freq
1 parent 00d88e9 commit 8a814a0

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

pandas/_libs/tslibs/offsets.pyx

+17-7
Original file line numberDiff line numberDiff line change
@@ -4673,7 +4673,9 @@ def _get_offset(name: str) -> BaseOffset:
46734673
offset = klass._from_name(*split[1:])
46744674
except (ValueError, TypeError, KeyError) as err:
46754675
# bad prefix or suffix
4676-
raise ValueError(INVALID_FREQ_ERR_MSG.format(name)) from err
4676+
raise ValueError(INVALID_FREQ_ERR_MSG.format(
4677+
f"{name}, failed to parse with error message: {repr(err)}")
4678+
)
46774679
# cache
46784680
_offset_map[name] = offset
46794681

@@ -4757,11 +4759,17 @@ cpdef to_offset(freq, bint is_period=False):
47574759
)
47584760
name = c_OFFSET_DEPR_FREQSTR[name]
47594761
if is_period is True and name in c_REVERSE_OFFSET_DEPR_FREQSTR:
4760-
raise ValueError(
4761-
f"for Period, please use "
4762-
f"\'{c_REVERSE_OFFSET_DEPR_FREQSTR.get(name)}\' "
4763-
f"instead of \'{name}\'"
4764-
)
4762+
if name.startswith("Y"):
4763+
raise ValueError(
4764+
f"for Period, please use \'Y{name[2:]}\' "
4765+
f"instead of \'{name}\'"
4766+
)
4767+
else:
4768+
raise ValueError(
4769+
f"for Period, please use "
4770+
f"\'{c_REVERSE_OFFSET_DEPR_FREQSTR.get(name)}\' "
4771+
f"instead of \'{name}\'"
4772+
)
47654773
elif is_period is True and name in c_OFFSET_DEPR_FREQSTR:
47664774
if name.startswith("A"):
47674775
warnings.warn(
@@ -4813,7 +4821,9 @@ cpdef to_offset(freq, bint is_period=False):
48134821
else:
48144822
delta = delta + offset
48154823
except (ValueError, TypeError) as err:
4816-
raise ValueError(INVALID_FREQ_ERR_MSG.format(freq)) from err
4824+
raise ValueError(INVALID_FREQ_ERR_MSG.format(
4825+
f"{freq}, failed to parse with error message: {repr(err)}")
4826+
)
48174827
else:
48184828
delta = None
48194829

pandas/tests/indexes/period/test_period.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def test_map(self):
273273
tm.assert_index_equal(result, exp)
274274

275275
def test_period_index_frequency_ME_error_message(self):
276-
msg = "Invalid frequency: 2ME"
276+
msg = "for Period, please use 'M' instead of 'ME'"
277277

278278
with pytest.raises(ValueError, match=msg):
279279
PeriodIndex(["2020-01-01", "2020-01-02"], freq="2ME")
@@ -302,7 +302,8 @@ def test_a_deprecated_from_time_series(self, freq_depr):
302302
@pytest.mark.parametrize("freq_depr", ["2ME", "2QE", "2YE"])
303303
def test_period_index_frequency_error_message(self, freq_depr):
304304
# GH#9586
305-
msg = f"Invalid frequency: {freq_depr}"
305+
msg = f"for Period, please use '{freq_depr[1:-1]}' "
306+
f"instead of '{freq_depr[1:]}'"
306307

307308
with pytest.raises(ValueError, match=msg):
308309
period_range("2020-01", "2020-05", freq=freq_depr)

pandas/tests/indexes/period/test_period_range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ def test_errors(self):
157157
period_range(start="2017Q1", periods="foo")
158158

159159
def test_period_range_frequency_ME_error_message(self):
160-
msg = "Invalid frequency: 2ME"
160+
msg = "for Period, please use 'M' instead of 'ME'"
161161
with pytest.raises(ValueError, match=msg):
162162
period_range(start="Jan-2000", end="Dec-2000", freq="2ME")

pandas/tests/resample/test_period_index.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -931,10 +931,11 @@ def test_resample_t_l_deprecated(self):
931931
tm.assert_series_equal(result, expected)
932932

933933

934-
@pytest.mark.parametrize("freq_depr", ["2ME", "2QE", "2QE-FEB", "2YE"])
934+
@pytest.mark.parametrize("freq_depr", ["2ME", "2QE", "2QE-FEB", "2YE", "2YE-MAR"])
935935
def test_resample_frequency_ME_QE_error_message(series_and_frame, freq_depr):
936936
# GH#9586
937-
msg = f"Invalid frequency: {freq_depr}"
937+
msg = f"for Period, please use '{freq_depr[1:2]}{freq_depr[3:]}' "
938+
f"instead of '{freq_depr[1:]}'"
938939

939940
obj = series_and_frame
940941
with pytest.raises(ValueError, match=msg):

pandas/tests/scalar/period/test_period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,6 @@ def test_invalid_frequency_error_message():
16321632

16331633

16341634
def test_invalid_frequency_period_error_message():
1635-
msg = "Invalid frequency: ME"
1635+
msg = "for Period, please use 'M' instead of 'ME'"
16361636
with pytest.raises(ValueError, match=msg):
16371637
Period("2012-01-02", freq="ME")

0 commit comments

Comments
 (0)