Skip to content

BUG: disallow constructing Period with freq=C #52567

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

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ Period
- Bug in :meth:`arrays.PeriodArray.map` and :meth:`PeriodIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
- Bug in :func:`read_csv` not processing empty strings as a null value, with ``engine="pyarrow"`` (:issue:`52087`)
- Bug in :func:`read_csv` returning ``object`` dtype columns instead of ``float64`` dtype columns with ``engine="pyarrow"`` for columns that are all null with ``engine="pyarrow"`` (:issue:`52087`)
- Bug in incorrectly allowing construction of :class:`Period` or :class:`PeriodDtype` with :class:`CustomBusinessDay` freq; use :class:`BusinessDay` instead (:issue:`52534`)
-

Plotting
^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,13 @@ cdef class CustomBusinessDay(BusinessDay):
["n", "normalize", "weekmask", "holidays", "calendar", "offset"]
)

@property
def _period_dtype_code(self):
# GH#52534
raise TypeError(
"CustomBusinessDay cannot be used with Period or PeriodDtype"
)

_apply_array = BaseOffset._apply_array

def __init__(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ def test_construction(self):
assert dt.freq == pd.tseries.offsets.Hour(26)
assert is_period_dtype(dt)

def test_cannot_use_custom_businessday(self):
# GH#52534
msg = "CustomBusinessDay cannot be used with Period or PeriodDtype"
with pytest.raises(TypeError, match=msg):
PeriodDtype("C")
with pytest.raises(TypeError, match=msg):
PeriodDtype(pd.offsets.CustomBusinessDay())

def test_subclass(self):
a = PeriodDtype("period[D]")
b = PeriodDtype("period[3D]")
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@


class TestPeriodConstruction:
def test_custom_business_day_freq_raises(self):
# GH#52534
msg = "CustomBusinessDay cannot be used with Period or PeriodDtype"
with pytest.raises(TypeError, match=msg):
Period("2023-04-10", freq="C")
with pytest.raises(TypeError, match=msg):
Period("2023-04-10", freq=offsets.CustomBusinessDay())

def test_from_td64nat_raises(self):
# GH#44507
td = NaT.to_numpy("m8[ns]")
Expand Down
1 change: 0 additions & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"BAS": "A",
"MS": "M",
"D": "D",
"C": "C",
"B": "B",
"T": "T",
"S": "S",
Expand Down