Skip to content

Backport PR #54721 on branch 2.1.x (BUG/WARN: Passing EA object to dtype instead of an instance) #54726

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ ExtensionArray
- Bug in :meth:`Series.rank` returning wrong order for small values with ``Float64`` dtype (:issue:`52471`)
- Bug in :meth:`Series.unique` for boolean ``ArrowDtype`` with ``NA`` values (:issue:`54667`)
- Bug in :meth:`~arrays.ArrowExtensionArray.__iter__` and :meth:`~arrays.ArrowExtensionArray.__getitem__` returning python datetime and timedelta objects for non-nano dtypes (:issue:`53326`)
- Bug when passing an :class:`ExtensionArray` subclass to ``dtype`` keywords. This will now raise a ``UserWarning`` to encourage passing an instance instead (:issue:`31356`, :issue:`54592`)
- Bug where the :class:`DataFrame` repr would not work when a column had an :class:`ArrowDtype` with a ``pyarrow.ExtensionDtype`` (:issue:`54063`)
- Bug where the ``__from_arrow__`` method of masked ExtensionDtypes (e.g. :class:`Float64Dtype`, :class:`BooleanDtype`) would not accept PyArrow arrays of type ``pyarrow.null()`` (:issue:`52223`)

Expand Down
9 changes: 9 additions & 0 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,15 @@ def pandas_dtype(dtype) -> DtypeObj:
# registered extension types
result = registry.find(dtype)
if result is not None:
if isinstance(result, type):
# GH 31356, GH 54592
warnings.warn(
f"Instantiating {result.__name__} without any arguments."
f"Pass a {result.__name__} instance to silence this warning.",
UserWarning,
stacklevel=find_stack_level(),
)
result = result()
return result

# try a numpy dtype
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,9 @@ def test_pandas_dtype_numpy_warning():
match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated",
):
pandas_dtype(np.integer)


def test_pandas_dtype_ea_not_instance():
# GH 31356 GH 54592
with tm.assert_produces_warning(UserWarning):
assert pandas_dtype(CategoricalDtype) == CategoricalDtype()