Skip to content

Commit 7256c13

Browse files
committed
move array_type -> construct_array_type
1 parent 6483c67 commit 7256c13

File tree

8 files changed

+62
-23
lines changed

8 files changed

+62
-23
lines changed

doc/source/whatsnew/v0.24.0.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ Backwards incompatible API changes
2626
ExtensionType Changes
2727
^^^^^^^^^^^^^^^^^^^^^
2828

29-
- ``ExtensionArray`` has gained the abstract methods ``.dropna()`` and ``.append()``, and attribute ``array_type`` (:issue:`21185`)
30-
- ``ExtensionDtype`` has gained the ability to instantiate from string dtypes, e.g. ``decimal`` would instantiate a registered ``DecimalDtype`` (:issue:`21185`)
29+
- ``ExtensionArray`` has gained the abstract methods ``.dropna()`` and ``.append()`` (:issue:`21185`)
30+
- ``ExtensionDtype`` has gained the ability to instantiate from string dtypes, e.g. ``decimal`` would instantiate a registered ``DecimalDtype``; furthermore
31+
the dtype has gained the ``construct_array_type`` (:issue:`21185`)
3132
- The ``ExtensionArray`` constructor, ``_from_sequence`` now take the keyword arg ``copy=False`` (:issue:`21185`)
3233

3334
.. _whatsnew_0240.api.other:

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _reconstruct_data(values, dtype, original):
154154
"""
155155
from pandas import Index
156156
if is_extension_array_dtype(dtype):
157-
values = dtype.array_type._from_sequence(values)
157+
values = dtype.construct_array_type(values)._from_sequence(values)
158158
elif is_datetime64tz_dtype(dtype) or is_period_dtype(dtype):
159159
values = Index(original)._shallow_copy(values, name=None)
160160
elif is_bool_dtype(dtype):

pandas/core/arrays/categorical.py

-4
Original file line numberDiff line numberDiff line change
@@ -2343,10 +2343,6 @@ def isin(self, values):
23432343
return algorithms.isin(self.codes, code_values)
23442344

23452345

2346-
# inform the Dtype about us
2347-
CategoricalDtype.array_type = Categorical
2348-
2349-
23502346
# The Series.cat accessor
23512347

23522348

pandas/core/dtypes/base.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ class ExtensionDtype(_DtypeOpsMixin):
109109
* name
110110
* construct_from_string
111111
112+
Optionally one can override construct_array_type for construction
113+
with the name of this dtype via the Registry
112114
113-
Optionally one can assign an array_type for construction with the name
114-
of this dtype via the Registry
115-
116-
* array_type
115+
* construct_array_type
117116
118117
The `na_value` class attribute can be used to set the default NA value
119118
for this type. :attr:`numpy.nan` is used by default.
@@ -124,8 +123,6 @@ class ExtensionDtype(_DtypeOpsMixin):
124123
provided for registering virtual subclasses.
125124
"""
126125

127-
array_type = None
128-
129126
def __str__(self):
130127
return self.name
131128

@@ -164,11 +161,19 @@ def name(self):
164161
"""
165162
raise AbstractMethodError(self)
166163

167-
@property
168-
def array_type(self):
164+
@classmethod
165+
def construct_array_type(cls, array):
169166
"""Return the array type associated with this dtype
167+
168+
Parameters
169+
----------
170+
string : str
171+
172+
Returns
173+
-------
174+
type
170175
"""
171-
raise AbstractMethodError(self)
176+
return type(array)
172177

173178
@classmethod
174179
def construct_from_string(cls, string):

pandas/core/dtypes/dtypes.py

+15
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ def _hash_categories(categories, ordered=True):
317317
else:
318318
return np.bitwise_xor.reduce(hashed)
319319

320+
@classmethod
321+
def construct_array_type(cls, array):
322+
"""Return the array type associated with this dtype
323+
324+
Parameters
325+
----------
326+
string : str
327+
328+
Returns
329+
-------
330+
type
331+
"""
332+
from pandas import Categorical
333+
return Categorical
334+
320335
@classmethod
321336
def construct_from_string(cls, string):
322337
""" attempt to construct this type from a string, raise a TypeError if

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4056,7 +4056,7 @@ def _try_cast(arr, take_fast_path):
40564056
ordered=dtype.ordered)
40574057
elif is_extension_array_dtype(dtype):
40584058
# create an extension array from its dtype
4059-
array_type = dtype.array_type
4059+
array_type = dtype.construct_array_type(subarr)
40604060
subarr = array_type(subarr, copy=copy)
40614061

40624062
elif dtype is not None and raise_cast_failure:

pandas/tests/extension/decimal/array.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class DecimalDtype(ExtensionDtype):
1515
name = 'decimal'
1616
na_value = decimal.Decimal('NaN')
1717

18+
@classmethod
19+
def construct_array_type(cls, array):
20+
"""Return the array type associated with this dtype
21+
22+
Parameters
23+
----------
24+
string : str
25+
26+
Returns
27+
-------
28+
type
29+
"""
30+
return DecimalArray
31+
1832
@classmethod
1933
def construct_from_string(cls, string):
2034
if string == cls.name:
@@ -101,8 +115,5 @@ def _concat_same_type(cls, to_concat):
101115
return cls(np.concatenate([x._data for x in to_concat]))
102116

103117

104-
DecimalDtype.array_type = DecimalArray
105-
106-
107118
def make_data():
108119
return [decimal.Decimal(random.random()) for _ in range(100)]

pandas/tests/extension/json/array.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ class JSONDtype(ExtensionDtype):
3232
# source compatibility with Py2.
3333
na_value = {}
3434

35+
@classmethod
36+
def construct_array_type(cls, array):
37+
"""Return the array type associated with this dtype
38+
39+
Parameters
40+
----------
41+
string : str
42+
43+
Returns
44+
-------
45+
type
46+
"""
47+
return JSONArray
48+
3549
@classmethod
3650
def construct_from_string(cls, string):
3751
if string == cls.name:
@@ -171,9 +185,6 @@ def _values_for_argsort(self):
171185
return np.array(frozen, dtype=object)[1:]
172186

173187

174-
JSONDtype.array_type = JSONArray
175-
176-
177188
def make_data():
178189
# TODO: Use a regular dict. See _NDFrameIndexer._setitem_with_indexer
179190
return [collections.UserDict([

0 commit comments

Comments
 (0)