Skip to content

Commit 9a0c382

Browse files
String dtype: more informative repr (keeping brief __str__)
1 parent 4ebd93b commit 9a0c382

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
@@ -123,6 +123,18 @@ def test_repr(dtype):
123123
assert repr(df.A.array) == expected
124124

125125

126+
def test_dtype_repr(dtype):
127+
if dtype.storage == "pyarrow":
128+
if dtype.na_value is pd.NA:
129+
assert repr(dtype) == "<StringDtype(na_value=<NA>)>"
130+
else:
131+
assert repr(dtype) == "<StringDtype(na_value=nan)>"
132+
elif dtype.na_value is pd.NA:
133+
assert repr(dtype) == "<StringDtype(storage='python', na_value=<NA>)>"
134+
else:
135+
assert repr(dtype) == "<StringDtype(storage='python', na_value=nan)>"
136+
137+
126138
def test_none_to_nan(cls, dtype):
127139
a = cls._from_sequence(["a", None, "b"], dtype=dtype)
128140
assert a[1] is not None

0 commit comments

Comments
 (0)