Skip to content

Commit d050408

Browse files
authored
BUG: displaying string dtypes not showing storage option (#50151)
1 parent bce9958 commit d050408

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ I/O
860860
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
861861
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
862862
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
863+
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
863864
- Bug in :func:`DataFrame.to_string` with ``header=False`` that printed the index name on the same line as the first row of the data (:issue:`49230`)
864865
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
865866
- Fixed issue where :func:`json_normalize` would incorrectly remove leading characters from column names that matched the ``sep`` argument (:issue:`49861`)

pandas/core/generic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6473,12 +6473,12 @@ def convert_dtypes(
64736473
2 3 z <NA> <NA> 20 200.0
64746474
64756475
>>> dfn.dtypes
6476-
a Int32
6477-
b string
6478-
c boolean
6479-
d string
6480-
e Int64
6481-
f Float64
6476+
a Int32
6477+
b string[python]
6478+
c boolean
6479+
d string[python]
6480+
e Int64
6481+
f Float64
64826482
dtype: object
64836483
64846484
Start with a Series of strings and missing data represented by ``np.nan``.

pandas/io/formats/format.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
DatetimeArray,
9090
TimedeltaArray,
9191
)
92+
from pandas.core.arrays.string_ import StringDtype
9293
from pandas.core.base import PandasObject
9394
import pandas.core.common as com
9495
from pandas.core.construction import extract_array
@@ -1395,6 +1396,8 @@ def _format(x):
13951396
return self.na_rep
13961397
elif isinstance(x, PandasObject):
13971398
return str(x)
1399+
elif isinstance(x, StringDtype):
1400+
return repr(x)
13981401
else:
13991402
# object dtype
14001403
return str(formatter(x))

pandas/tests/io/formats/test_to_string.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
import pandas.util._test_decorators as td
9+
810
from pandas import (
911
DataFrame,
1012
Series,
@@ -338,3 +340,20 @@ def test_to_string_max_rows_zero(data, expected):
338340
# GH35394
339341
result = DataFrame(data=data).to_string(max_rows=0)
340342
assert result == expected
343+
344+
345+
@td.skip_if_no("pyarrow")
346+
def test_to_string_string_dtype():
347+
# GH#50099
348+
df = DataFrame({"x": ["foo", "bar", "baz"], "y": ["a", "b", "c"], "z": [1, 2, 3]})
349+
df = df.astype(
350+
{"x": "string[pyarrow]", "y": "string[python]", "z": "int64[pyarrow]"}
351+
)
352+
result = df.dtypes.to_string()
353+
expected = dedent(
354+
"""\
355+
x string[pyarrow]
356+
y string[python]
357+
z int64[pyarrow]"""
358+
)
359+
assert result == expected

0 commit comments

Comments
 (0)