Skip to content

Commit 5222d75

Browse files
committed
API: Make ExtensionDtype.construct_array_type a method
This allows a single dtype to support multiple array classes. For arrow-backed strings, we'll likely want a separate array class for ease of implementation, clarity. But we'll have a parametrized dtype. ```python class StringDtype: def __init__(self, storage="python"): self.storage = storage def construct_array_type(self): # regular method if self.storage == "python": return StringArray else: return ArrowStringArray ``` Closes pandas-dev#36126
1 parent e288b2b commit 5222d75

File tree

13 files changed

+23
-32
lines changed

13 files changed

+23
-32
lines changed

doc/source/whatsnew/v1.2.0.rst

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ Other enhancements
5757
-
5858
-
5959

60+
.. _whatsnew_120.api_breaking.experimental:
61+
62+
Changes to experimental APIs
63+
----------------------------
64+
65+
- :meth:`pandas.api.extensions.ExtensionDtype.construct_array_type` has changed from a classmethod to a regular method to support one dtype being used for multiple arrays. To migrate, change your definition to a regular method and ensure that your method is called on instances rather than the class (:issue:`36126`).
66+
6067
.. _whatsnew_120.api_breaking.python:
6168

6269
Increased minimum version for Python

pandas/core/arrays/boolean.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ def kind(self) -> str:
7070
def numpy_dtype(self) -> np.dtype:
7171
return np.dtype("bool")
7272

73-
@classmethod
74-
def construct_array_type(cls) -> Type["BooleanArray"]:
73+
def construct_array_type(self) -> Type["BooleanArray"]:
7574
"""
7675
Return the array type associated with this dtype.
7776

pandas/core/arrays/integer.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def itemsize(self) -> int:
8080
""" Return the number of bytes in this dtype """
8181
return self.numpy_dtype.itemsize
8282

83-
@classmethod
84-
def construct_array_type(cls) -> Type["IntegerArray"]:
83+
def construct_array_type(self) -> Type["IntegerArray"]:
8584
"""
8685
Return the array type associated with this dtype.
8786

pandas/core/arrays/masked.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class BaseMaskedDtype(ExtensionDtype):
4040
def numpy_dtype(self) -> np.dtype:
4141
raise AbstractMethodError
4242

43-
@classmethod
44-
def construct_array_type(cls) -> Type["BaseMaskedArray"]:
43+
def construct_array_type(self) -> Type["BaseMaskedArray"]:
4544
"""
4645
Return the array type associated with this dtype.
4746

pandas/core/arrays/numpy_.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def construct_from_string(cls, string: str) -> "PandasDtype":
9494
raise TypeError(msg) from err
9595
return cls(dtype)
9696

97-
@classmethod
98-
def construct_array_type(cls) -> Type["PandasArray"]:
97+
def construct_array_type(self) -> Type["PandasArray"]:
9998
"""
10099
Return the array type associated with this dtype.
101100

pandas/core/arrays/sparse/dtype.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ def name(self):
171171
def __repr__(self) -> str:
172172
return self.name
173173

174-
@classmethod
175-
def construct_array_type(cls) -> Type["SparseArray"]:
174+
def construct_array_type(self) -> Type["SparseArray"]:
176175
"""
177176
Return the array type associated with this dtype.
178177

pandas/core/dtypes/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ def names(self) -> Optional[List[str]]:
187187
"""
188188
return None
189189

190-
@classmethod
191-
def construct_array_type(cls) -> Type["ExtensionArray"]:
190+
def construct_array_type(self) -> Type["ExtensionArray"]:
192191
"""
193192
Return the array type associated with this dtype.
194193

pandas/core/dtypes/dtypes.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ def _hash_categories(categories, ordered: Ordered = True) -> int:
430430
hashed = _combine_hash_arrays(iter(cat_array), num_items=len(cat_array))
431431
return np.bitwise_xor.reduce(hashed)
432432

433-
@classmethod
434-
def construct_array_type(cls) -> Type["Categorical"]:
433+
def construct_array_type(self) -> Type["Categorical"]:
435434
"""
436435
Return the array type associated with this dtype.
437436
@@ -679,8 +678,7 @@ def tz(self):
679678
"""
680679
return self._tz
681680

682-
@classmethod
683-
def construct_array_type(cls) -> Type["DatetimeArray"]:
681+
def construct_array_type(self) -> Type["DatetimeArray"]:
684682
"""
685683
Return the array type associated with this dtype.
686684
@@ -922,8 +920,7 @@ def is_dtype(cls, dtype: object) -> bool:
922920
return False
923921
return super().is_dtype(dtype)
924922

925-
@classmethod
926-
def construct_array_type(cls) -> Type["PeriodArray"]:
923+
def construct_array_type(self) -> Type["PeriodArray"]:
927924
"""
928925
Return the array type associated with this dtype.
929926
@@ -1047,8 +1044,7 @@ def subtype(self):
10471044
"""
10481045
return self._subtype
10491046

1050-
@classmethod
1051-
def construct_array_type(cls) -> Type["IntervalArray"]:
1047+
def construct_array_type(self) -> Type["IntervalArray"]:
10521048
"""
10531049
Return the array type associated with this dtype.
10541050

pandas/tests/arrays/test_array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ def test_scalar_raises():
277277
class DecimalDtype2(DecimalDtype):
278278
name = "decimal2"
279279

280-
@classmethod
281-
def construct_array_type(cls):
280+
def construct_array_type(self):
282281
"""
283282
Return the array type associated with this dtype.
284283

pandas/tests/extension/arrow/arrays.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class ArrowBoolDtype(ExtensionDtype):
3131
name = "arrow_bool"
3232
na_value = pa.NULL
3333

34-
@classmethod
35-
def construct_array_type(cls) -> Type["ArrowBoolArray"]:
34+
def construct_array_type(self) -> Type["ArrowBoolArray"]:
3635
"""
3736
Return the array type associated with this dtype.
3837
@@ -55,8 +54,7 @@ class ArrowStringDtype(ExtensionDtype):
5554
name = "arrow_string"
5655
na_value = pa.NULL
5756

58-
@classmethod
59-
def construct_array_type(cls) -> Type["ArrowStringArray"]:
57+
def construct_array_type(self) -> Type["ArrowStringArray"]:
6058
"""
6159
Return the array type associated with this dtype.
6260

pandas/tests/extension/decimal/array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def __init__(self, context=None):
2828
def __repr__(self) -> str:
2929
return f"DecimalDtype(context={self.context})"
3030

31-
@classmethod
32-
def construct_array_type(cls) -> Type["DecimalArray"]:
31+
def construct_array_type(self) -> Type["DecimalArray"]:
3332
"""
3433
Return the array type associated with this dtype.
3534

pandas/tests/extension/json/array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ class JSONDtype(ExtensionDtype):
3232
name = "json"
3333
na_value: Mapping[str, Any] = UserDict()
3434

35-
@classmethod
36-
def construct_array_type(cls) -> Type["JSONArray"]:
35+
def construct_array_type(self) -> Type["JSONArray"]:
3736
"""
3837
Return the array type associated with this dtype.
3938

pandas/tests/extension/list/array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class ListDtype(ExtensionDtype):
2121
name = "list"
2222
na_value = np.nan
2323

24-
@classmethod
25-
def construct_array_type(cls) -> Type["ListArray"]:
24+
def construct_array_type(self) -> Type["ListArray"]:
2625
"""
2726
Return the array type associated with this dtype.
2827

0 commit comments

Comments
 (0)