Skip to content
Merged
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: 7 additions & 4 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ class StringDtype(StorageExtensionDtype):
Examples
--------
>>> pd.StringDtype()
<StringDtype(storage='python', na_value=<NA>)>
string[python]

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

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

def __repr__(self) -> str:
storage = "" if self.storage == "pyarrow" else "storage='python', "
return f"<StringDtype({storage}na_value={self._na_value})>"
if self._na_value is libmissing.NA:
return f"{self.name}[{self.storage}]"
else:
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 @@ -7021,12 +7021,12 @@ def convert_dtypes(
2 3 z <NA> <NA> 20 200.0

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

Start with a Series of strings and missing data represented by ``np.nan``.
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
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 @@ -1231,6 +1232,8 @@ def _format(x):
return self.na_rep
elif isinstance(x, PandasObject):
return str(x)
elif isinstance(x, StringDtype) and x.na_value is NA:
return repr(x)
else:
# object dtype
return str(formatter(x))
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ def test_repr(dtype):
def test_dtype_repr(dtype):
if dtype.storage == "pyarrow":
if dtype.na_value is pd.NA:
assert repr(dtype) == "<StringDtype(na_value=<NA>)>"
assert repr(dtype) == "string[pyarrow]"
else:
assert repr(dtype) == "<StringDtype(na_value=nan)>"
elif dtype.na_value is pd.NA:
assert repr(dtype) == "<StringDtype(storage='python', na_value=<NA>)>"
assert repr(dtype) == "string[python]"
else:
assert repr(dtype) == "<StringDtype(storage='python', na_value=nan)>"

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 @@ -758,9 +758,9 @@ def test_to_string_string_dtype(self):
result = df.dtypes.to_string()
expected = dedent(
"""\
x string
y string
z int64[pyarrow]"""
x string[pyarrow]
y string[python]
z int64[pyarrow]"""
)
assert result == expected

Expand Down
Loading