8
8
from .base import ExtensionDtype , _DtypeOpsMixin
9
9
10
10
11
+ def register_extension_dtype (cls ):
12
+ """Class decorator to register an ExtensionType with pandas.
13
+
14
+ .. versionadded:: 0.24.0
15
+
16
+ This enables operations like ``.astype(name)`` for the name
17
+ of the ExtensionDtype.
18
+
19
+ Examples
20
+ --------
21
+ >>> from pandas.api.extensions import register_extension_dtype
22
+ >>> from pandas.api.extensions import ExtensionDtype
23
+ >>> @register_extension_dtype
24
+ ... class MyExtensionDtype(ExtensionDtype):
25
+ ... pass
26
+ """
27
+ registry .register (cls )
28
+ return cls
29
+
30
+
11
31
class Registry (object ):
12
32
"""
13
33
Registry for dtype inference
@@ -17,10 +37,6 @@ class Registry(object):
17
37
18
38
Multiple extension types can be registered.
19
39
These are tried in order.
20
-
21
- Examples
22
- --------
23
- registry.register(MyExtensionDtype)
24
40
"""
25
41
def __init__ (self ):
26
42
self .dtypes = []
@@ -65,9 +81,6 @@ def find(self, dtype):
65
81
66
82
67
83
registry = Registry ()
68
- # TODO(Extension): remove the second registry once all internal extension
69
- # dtypes are real extension dtypes.
70
- _pandas_registry = Registry ()
71
84
72
85
73
86
class PandasExtensionDtype (_DtypeOpsMixin ):
@@ -145,6 +158,7 @@ class CategoricalDtypeType(type):
145
158
pass
146
159
147
160
161
+ @register_extension_dtype
148
162
class CategoricalDtype (PandasExtensionDtype , ExtensionDtype ):
149
163
"""
150
164
Type for categorical data with the categories and orderedness
@@ -692,6 +706,7 @@ class IntervalDtypeType(type):
692
706
pass
693
707
694
708
709
+ @register_extension_dtype
695
710
class IntervalDtype (PandasExtensionDtype , ExtensionDtype ):
696
711
"""
697
712
A Interval duck-typed class, suitable for holding an interval
@@ -824,8 +839,9 @@ def is_dtype(cls, dtype):
824
839
return super (IntervalDtype , cls ).is_dtype (dtype )
825
840
826
841
827
- # register the dtypes in search order
828
- registry .register (IntervalDtype )
829
- registry .register (CategoricalDtype )
842
+ # TODO(Extension): remove the second registry once all internal extension
843
+ # dtypes are real extension dtypes.
844
+ _pandas_registry = Registry ()
845
+
830
846
_pandas_registry .register (DatetimeTZDtype )
831
847
_pandas_registry .register (PeriodDtype )
0 commit comments