15
15
import pytz
16
16
17
17
from pandas ._libs .interval import Interval
18
+ from pandas ._libs .properties import cache_readonly
18
19
from pandas ._libs .tslibs import (
19
20
BaseOffset ,
20
21
NaT ,
@@ -81,7 +82,7 @@ class PandasExtensionDtype(ExtensionDtype):
81
82
base : DtypeObj | None = None
82
83
isbuiltin = 0
83
84
isnative = 0
84
- _cache : dict [str_type , PandasExtensionDtype ] = {}
85
+ _cache_dtypes : dict [str_type , PandasExtensionDtype ] = {}
85
86
86
87
def __str__ (self ) -> str_type :
87
88
"""
@@ -105,7 +106,7 @@ def __getstate__(self) -> dict[str_type, Any]:
105
106
@classmethod
106
107
def reset_cache (cls ) -> None :
107
108
""" clear the cache """
108
- cls ._cache = {}
109
+ cls ._cache_dtypes = {}
109
110
110
111
111
112
class CategoricalDtypeType (type ):
@@ -177,7 +178,7 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
177
178
str = "|O08"
178
179
base = np .dtype ("O" )
179
180
_metadata = ("categories" , "ordered" )
180
- _cache : dict [str_type , PandasExtensionDtype ] = {}
181
+ _cache_dtypes : dict [str_type , PandasExtensionDtype ] = {}
181
182
182
183
def __init__ (self , categories = None , ordered : Ordered = False ):
183
184
self ._finalize (categories , ordered , fastpath = False )
@@ -355,7 +356,7 @@ def __hash__(self) -> int:
355
356
else :
356
357
return - 2
357
358
# We *do* want to include the real self.ordered here
358
- return int (self ._hash_categories ( self . categories , self . ordered ) )
359
+ return int (self ._hash_categories )
359
360
360
361
def __eq__ (self , other : Any ) -> bool :
361
362
"""
@@ -429,14 +430,17 @@ def __repr__(self) -> str_type:
429
430
data = data .rstrip (", " )
430
431
return f"CategoricalDtype(categories={ data } , ordered={ self .ordered } )"
431
432
432
- @staticmethod
433
- def _hash_categories (categories , ordered : Ordered = True ) -> int :
433
+ @cache_readonly
434
+ def _hash_categories (self ) -> int :
434
435
from pandas .core .util .hashing import (
435
436
combine_hash_arrays ,
436
437
hash_array ,
437
438
hash_tuples ,
438
439
)
439
440
441
+ categories = self .categories
442
+ ordered = self .ordered
443
+
440
444
if len (categories ) and isinstance (categories [0 ], tuple ):
441
445
# assumes if any individual category is a tuple, then all our. ATM
442
446
# I don't really want to support just some of the categories being
@@ -671,7 +675,7 @@ class DatetimeTZDtype(PandasExtensionDtype):
671
675
na_value = NaT
672
676
_metadata = ("unit" , "tz" )
673
677
_match = re .compile (r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]" )
674
- _cache : dict [str_type , PandasExtensionDtype ] = {}
678
+ _cache_dtypes : dict [str_type , PandasExtensionDtype ] = {}
675
679
676
680
def __init__ (self , unit : str_type | DatetimeTZDtype = "ns" , tz = None ):
677
681
if isinstance (unit , DatetimeTZDtype ):
@@ -837,7 +841,7 @@ class PeriodDtype(dtypes.PeriodDtypeBase, PandasExtensionDtype):
837
841
num = 102
838
842
_metadata = ("freq" ,)
839
843
_match = re .compile (r"(P|p)eriod\[(?P<freq>.+)\]" )
840
- _cache : dict [str_type , PandasExtensionDtype ] = {}
844
+ _cache_dtypes : dict [str_type , PandasExtensionDtype ] = {}
841
845
842
846
def __new__ (cls , freq = None ):
843
847
"""
@@ -859,12 +863,12 @@ def __new__(cls, freq=None):
859
863
freq = cls ._parse_dtype_strict (freq )
860
864
861
865
try :
862
- return cls ._cache [freq .freqstr ]
866
+ return cls ._cache_dtypes [freq .freqstr ]
863
867
except KeyError :
864
868
dtype_code = freq ._period_dtype_code
865
869
u = dtypes .PeriodDtypeBase .__new__ (cls , dtype_code )
866
870
u ._freq = freq
867
- cls ._cache [freq .freqstr ] = u
871
+ cls ._cache_dtypes [freq .freqstr ] = u
868
872
return u
869
873
870
874
def __reduce__ (self ):
@@ -1042,7 +1046,7 @@ class IntervalDtype(PandasExtensionDtype):
1042
1046
_match = re .compile (
1043
1047
r"(I|i)nterval\[(?P<subtype>[^,]+)(, (?P<closed>(right|left|both|neither)))?\]"
1044
1048
)
1045
- _cache : dict [str_type , PandasExtensionDtype ] = {}
1049
+ _cache_dtypes : dict [str_type , PandasExtensionDtype ] = {}
1046
1050
1047
1051
def __new__ (cls , subtype = None , closed : str_type | None = None ):
1048
1052
from pandas .core .dtypes .common import (
@@ -1099,12 +1103,12 @@ def __new__(cls, subtype=None, closed: str_type | None = None):
1099
1103
1100
1104
key = str (subtype ) + str (closed )
1101
1105
try :
1102
- return cls ._cache [key ]
1106
+ return cls ._cache_dtypes [key ]
1103
1107
except KeyError :
1104
1108
u = object .__new__ (cls )
1105
1109
u ._subtype = subtype
1106
1110
u ._closed = closed
1107
- cls ._cache [key ] = u
1111
+ cls ._cache_dtypes [key ] = u
1108
1112
return u
1109
1113
1110
1114
@property
0 commit comments