Skip to content

Commit 2b1ea68

Browse files
committed
move array_type -> construct_array_type
1 parent 69b958b commit 2b1ea68

File tree

8 files changed

+62
-23
lines changed

8 files changed

+62
-23
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ Datetimelike API Changes
3535
ExtensionType Changes
3636
^^^^^^^^^^^^^^^^^^^^^
3737

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

4243
.. _whatsnew_0240.api.other:

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,10 +2354,6 @@ def isin(self, values):
23542354
return algorithms.isin(self.codes, code_values)
23552355

23562356

2357-
# inform the Dtype about us
2358-
CategoricalDtype.array_type = Categorical
2359-
2360-
23612357
# The Series.cat accessor
23622358

23632359

pandas/core/dtypes/base.py

Lines changed: 14 additions & 9 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4062,7 +4062,7 @@ def _try_cast(arr, take_fast_path):
40624062
ordered=dtype.ordered)
40634063
elif is_extension_array_dtype(dtype):
40644064
# create an extension array from its dtype
4065-
array_type = dtype.array_type
4065+
array_type = dtype.construct_array_type(subarr)
40664066
subarr = array_type(subarr, copy=copy)
40674067

40684068
elif dtype is not None and raise_cast_failure:

pandas/tests/extension/decimal/array.py

Lines changed: 14 additions & 3 deletions
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

Lines changed: 14 additions & 3 deletions
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)