Skip to content

API: Make ExtensionDtype.construct_array_type a method #36136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ Other enhancements
-
-

.. _whatsnew_120.api_breaking.experimental:

Changes to experimental APIs
----------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs ~~~~~~~~~~~~~~~~~~~~~~~~ type header


- :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`).
Copy link
Member

@jorisvandenbossche jorisvandenbossche Sep 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, as long as you are not calling it yourself on the class object, and only implementing it, it shouldn't directly break existing dtypes ? (a class method can also be called from an instance)

(otherwise I think we should do it in a non-breaking way, eg by taking a different name)


.. _whatsnew_120.api_breaking.python:

Increased minimum version for Python
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def kind(self) -> str:
def numpy_dtype(self) -> np.dtype:
return np.dtype("bool")

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def itemsize(self) -> int:
""" Return the number of bytes in this dtype """
return self.numpy_dtype.itemsize

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class BaseMaskedDtype(ExtensionDtype):
def numpy_dtype(self) -> np.dtype:
raise AbstractMethodError

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ def construct_from_string(cls, string: str) -> "PandasDtype":
raise TypeError(msg) from err
return cls(dtype)

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ def name(self):
def __repr__(self) -> str:
return self.name

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def names(self) -> Optional[List[str]]:
"""
return None

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

Expand Down
12 changes: 4 additions & 8 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ def _hash_categories(categories, ordered: Ordered = True) -> int:
hashed = _combine_hash_arrays(iter(cat_array), num_items=len(cat_array))
return np.bitwise_xor.reduce(hashed)

@classmethod
def construct_array_type(cls) -> Type["Categorical"]:
def construct_array_type(self) -> Type["Categorical"]:
"""
Return the array type associated with this dtype.

Expand Down Expand Up @@ -679,8 +678,7 @@ def tz(self):
"""
return self._tz

@classmethod
def construct_array_type(cls) -> Type["DatetimeArray"]:
def construct_array_type(self) -> Type["DatetimeArray"]:
"""
Return the array type associated with this dtype.

Expand Down Expand Up @@ -922,8 +920,7 @@ def is_dtype(cls, dtype: object) -> bool:
return False
return super().is_dtype(dtype)

@classmethod
def construct_array_type(cls) -> Type["PeriodArray"]:
def construct_array_type(self) -> Type["PeriodArray"]:
"""
Return the array type associated with this dtype.

Expand Down Expand Up @@ -1047,8 +1044,7 @@ def subtype(self):
"""
return self._subtype

@classmethod
def construct_array_type(cls) -> Type["IntervalArray"]:
def construct_array_type(self) -> Type["IntervalArray"]:
"""
Return the array type associated with this dtype.

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,7 @@ def test_scalar_raises():
class DecimalDtype2(DecimalDtype):
name = "decimal2"

@classmethod
def construct_array_type(cls):
def construct_array_type(self):
"""
Return the array type associated with this dtype.

Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/extension/arrow/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class ArrowBoolDtype(ExtensionDtype):
name = "arrow_bool"
na_value = pa.NULL

@classmethod
def construct_array_type(cls) -> Type["ArrowBoolArray"]:
def construct_array_type(self) -> Type["ArrowBoolArray"]:
"""
Return the array type associated with this dtype.

Expand All @@ -55,8 +54,7 @@ class ArrowStringDtype(ExtensionDtype):
name = "arrow_string"
na_value = pa.NULL

@classmethod
def construct_array_type(cls) -> Type["ArrowStringArray"]:
def construct_array_type(self) -> Type["ArrowStringArray"]:
"""
Return the array type associated with this dtype.

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def __init__(self, context=None):
def __repr__(self) -> str:
return f"DecimalDtype(context={self.context})"

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class JSONDtype(ExtensionDtype):
name = "json"
na_value: Mapping[str, Any] = UserDict()

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/extension/list/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class ListDtype(ExtensionDtype):
name = "list"
na_value = np.nan

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

Expand Down