Skip to content
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

String dtype: more informative repr (keeping brief __str__) #61148

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 4 additions & 7 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class StringDtype(StorageExtensionDtype):
Examples
--------
>>> pd.StringDtype()
string[python]
<StringDtype(storage='python', na_value=<NA>)>

>>> pd.StringDtype(storage="pyarrow")
string[pyarrow]
<StringDtype(na_value=<NA>)>
"""

@property
Expand Down Expand Up @@ -198,11 +198,8 @@ def __init__(
self._na_value = na_value

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

def __eq__(self, other: object) -> bool:
# we need to override the base class __eq__ because na_value (NA or NaN)
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6809,12 +6809,12 @@ def convert_dtypes(
2 3 z <NA> <NA> 20 200.0

>>> dfn.dtypes
a Int32
b string[python]
c boolean
d string[python]
e Int64
f Float64
a Int32
b string
c boolean
d string
e Int64
f Float64
dtype: object

Start with a Series of strings and missing data represented by ``np.nan``.
Expand Down
3 changes: 0 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
ExtensionArray,
TimedeltaArray,
)
from pandas.core.arrays.string_ import StringDtype
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.indexes.api import (
Expand Down Expand Up @@ -1218,8 +1217,6 @@ def _format(x):
return self.na_rep
elif isinstance(x, PandasObject):
return str(x)
elif isinstance(x, StringDtype):
return repr(x)
else:
# object dtype
return str(formatter(x))
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def test_repr(dtype):
assert repr(df.A.array) == expected


def test_dtype_repr(dtype):
if dtype.storage == "pyarrow":
if dtype.na_value is pd.NA:
assert repr(dtype) == "<StringDtype(na_value=<NA>)>"
else:
assert repr(dtype) == "<StringDtype(na_value=nan)>"
elif dtype.na_value is pd.NA:
assert repr(dtype) == "<StringDtype(storage='python', na_value=<NA>)>"
else:
assert repr(dtype) == "<StringDtype(storage='python', na_value=nan)>"


def test_none_to_nan(cls, dtype):
a = cls._from_sequence(["a", None, "b"], dtype=dtype)
assert a[1] is not None
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,9 @@ def test_to_string_string_dtype(self):
result = df.dtypes.to_string()
expected = dedent(
"""\
x string[pyarrow]
y string[python]
z int64[pyarrow]"""
x string
y string
z int64[pyarrow]"""
)
assert result == expected

Expand Down