Skip to content

Commit a180f0f

Browse files
authored
BUG/WARN: Passing EA object to dtype instead of an instance (#54721)
1 parent 867cae3 commit a180f0f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ ExtensionArray
837837
- Bug in :meth:`Series.rank` returning wrong order for small values with ``Float64`` dtype (:issue:`52471`)
838838
- Bug in :meth:`Series.unique` for boolean ``ArrowDtype`` with ``NA`` values (:issue:`54667`)
839839
- Bug in :meth:`~arrays.ArrowExtensionArray.__iter__` and :meth:`~arrays.ArrowExtensionArray.__getitem__` returning python datetime and timedelta objects for non-nano dtypes (:issue:`53326`)
840+
- 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`)
840841
- Bug where the :class:`DataFrame` repr would not work when a column had an :class:`ArrowDtype` with a ``pyarrow.ExtensionDtype`` (:issue:`54063`)
841842
- 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`)
842843

pandas/core/dtypes/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,15 @@ def pandas_dtype(dtype) -> DtypeObj:
16141614
# registered extension types
16151615
result = registry.find(dtype)
16161616
if result is not None:
1617+
if isinstance(result, type):
1618+
# GH 31356, GH 54592
1619+
warnings.warn(
1620+
f"Instantiating {result.__name__} without any arguments."
1621+
f"Pass a {result.__name__} instance to silence this warning.",
1622+
UserWarning,
1623+
stacklevel=find_stack_level(),
1624+
)
1625+
result = result()
16171626
return result
16181627

16191628
# try a numpy dtype

pandas/tests/dtypes/test_common.py

+6
Original file line numberDiff line numberDiff line change
@@ -782,3 +782,9 @@ def test_pandas_dtype_numpy_warning():
782782
match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated",
783783
):
784784
pandas_dtype(np.integer)
785+
786+
787+
def test_pandas_dtype_ea_not_instance():
788+
# GH 31356 GH 54592
789+
with tm.assert_produces_warning(UserWarning):
790+
assert pandas_dtype(CategoricalDtype) == CategoricalDtype()

0 commit comments

Comments
 (0)