Skip to content

Commit

Permalink
Backport PR #54721 on branch 2.1.x (BUG/WARN: Passing EA object to dt…
Browse files Browse the repository at this point in the history
…ype instead of an instance) (#54726)

Backport PR #54721: BUG/WARN: Passing EA object to dtype instead of an instance

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and mroeschke committed Aug 24, 2023
1 parent 2ad36cc commit c5adf1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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()

0 comments on commit c5adf1a

Please sign in to comment.