Skip to content

Commit b966986

Browse files
String dtype: more informative repr (keeping brief __str__)
1 parent 64e8f2c commit b966986

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pandas/core/arrays/string_.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ def __init__(
198198
self._na_value = na_value
199199

200200
def __repr__(self) -> str:
201-
if self._na_value is libmissing.NA:
202-
return f"{self.name}[{self.storage}]"
203-
else:
204-
# TODO add more informative repr
205-
return self.name
201+
storage = "" if self.storage == "pyarrow" else "storage='python', "
202+
return f"<StringDtype({storage}na_value={self._na_value})>"
206203

207204
def __eq__(self, other: object) -> bool:
208205
# we need to override the base class __eq__ because na_value (NA or NaN)

pandas/tests/arrays/string_/test_string.py

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def test_repr(dtype):
103103
assert repr(df.A.array) == expected
104104

105105

106+
def test_dtype_repr(dtype):
107+
if dtype.storage == "pyarrow":
108+
if dtype.na_value is pd.NA:
109+
assert repr(dtype) == "<StringDtype(na_value=<NA>)>"
110+
else:
111+
assert repr(dtype) == "<StringDtype(na_value=nan)>"
112+
elif dtype.na_value is pd.NA:
113+
assert repr(dtype) == "<StringDtype(storage='python', na_value=<NA>)>"
114+
else:
115+
assert repr(dtype) == "<StringDtype(storage='python', na_value=nan)>"
116+
117+
106118
def test_none_to_nan(cls, dtype):
107119
a = cls._from_sequence(["a", None, "b"], dtype=dtype)
108120
assert a[1] is not None

0 commit comments

Comments
 (0)