Skip to content

BUG: PeriodDtype incorrectly caching different DateOffset freqs #52991

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 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ Period
^^^^^^
- :meth:`PeriodIndex.map` with ``na_action="ignore"`` now works as expected (:issue:`51644`)
- Bug in :class:`PeriodDtype` constructor failing to raise ``TypeError`` when no argument is passed or when ``None`` is passed (:issue:`27388`)
- Bug in :class:`PeriodDtype` constructor incorrectly returning the same ``normalize`` for different :class:`DateOffset` ``freq`` inputs (:issue:`24121`)
- Bug in :class:`PeriodDtype` constructor raising ``ValueError`` instead of ``TypeError`` when an invalid type is passed (:issue:`51790`)
- 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 :meth:`arrays.PeriodArray.map` and :meth:`PeriodIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
- Bug in incorrectly allowing construction of :class:`Period` or :class:`PeriodDtype` with :class:`CustomBusinessDay` freq; use :class:`BusinessDay` instead (:issue:`52534`)
-

Plotting
^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
num = 102
_metadata = ("freq",)
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
_cache_dtypes: dict[int, PandasExtensionDtype] = {}
__hash__ = PeriodDtypeBase.__hash__
_freq: BaseOffset

Expand All @@ -916,12 +916,12 @@ def __new__(cls, freq):
freq = cls._parse_dtype_strict(freq)

try:
return cls._cache_dtypes[freq.freqstr]
return cls._cache_dtypes[hash(freq)]
except KeyError:
dtype_code = freq._period_dtype_code
u = PeriodDtypeBase.__new__(cls, dtype_code, freq.n)
u._freq = freq
cls._cache_dtypes[freq.freqstr] = u
cls._cache_dtypes[hash(freq)] = u
return u

def __reduce__(self):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/arrays/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ def test_registered():
assert result == expected


def test_perioddtype_caching_dateoffset_normalize():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this go with other PeriodDtype tests (as opposed to the Array)

# GH 24121
per_d = PeriodDtype(pd.offsets.YearEnd(normalize=True))
assert per_d.freq.normalize

per_d2 = PeriodDtype(pd.offsets.YearEnd(normalize=False))
assert not per_d2.freq.normalize


# ----------------------------------------------------------------------------
# period_array

Expand Down