Skip to content

BUG: displaying string dtypes not showing storage option #50151

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

Merged
merged 5 commits into from
Dec 13, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ I/O
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
- 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`)
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
- Fixed issue where :func:`json_normalize` would incorrectly remove leading characters from column names that matched the ``sep`` argument (:issue:`49861`)
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6473,12 +6473,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 @@ -89,6 +89,7 @@
DatetimeArray,
TimedeltaArray,
)
from pandas.core.arrays.string_ import StringDtype
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.construction import extract_array
Expand Down Expand Up @@ -1395,6 +1396,8 @@ 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
19 changes: 19 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -338,3 +340,20 @@ def test_to_string_max_rows_zero(data, expected):
# GH35394
result = DataFrame(data=data).to_string(max_rows=0)
assert result == expected


@td.skip_if_no("pyarrow")
def test_to_string_string_dtype():
# GH#50099
df = DataFrame({"x": ["foo", "bar", "baz"], "y": ["a", "b", "c"], "z": [1, 2, 3]})
df = df.astype(
{"x": "string[pyarrow]", "y": "string[python]", "z": "int64[pyarrow]"}
)
result = df.dtypes.to_string()
expected = dedent(
"""\
x string[pyarrow]
y string[python]
z int64[pyarrow]"""
)
assert result == expected