Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3832e85

Browse files
String dtype: more informative repr (keeping brief __str__) (#61148)
* String dtype: more informative repr (keeping brief __str__)* fix display in series* update doctest* update docstring* fixup
1 parent9c5b9ee commit3832e85

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

‎pandas/core/arrays/string_.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ class StringDtype(StorageExtensionDtype):
123123
Examples
124124
--------
125125
>>> pd.StringDtype()
126-
string[python]
126+
<StringDtype(storage='python', na_value=<NA>)>
127127
128128
>>> pd.StringDtype(storage="pyarrow")
129-
string[pyarrow]
129+
<StringDtype(na_value=<NA>)>
130130
"""
131131

132132
@property
@@ -198,11 +198,8 @@ def __init__(
198198
self._na_value=na_value
199199

200200
def__repr__(self)->str:
201-
ifself._na_valueislibmissing.NA:
202-
returnf"{self.name}[{self.storage}]"
203-
else:
204-
# TODO add more informative repr
205-
returnself.name
201+
storage=""ifself.storage=="pyarrow"else"storage='python', "
202+
returnf"<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/core/generic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6819,12 +6819,12 @@ def convert_dtypes(
68196819
2 3 z <NA> <NA> 20 200.0
68206820
68216821
>>> dfn.dtypes
6822-
aInt32
6823-
b string[python]
6824-
cboolean
6825-
d string[python]
6826-
eInt64
6827-
fFloat64
6822+
a Int32
6823+
bstring
6824+
c boolean
6825+
dstring
6826+
e Int64
6827+
f Float64
68286828
dtype: object
68296829
68306830
Start with a Series of strings and missing data represented by ``np.nan``.

‎pandas/io/formats/format.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
ExtensionArray,
6868
TimedeltaArray,
6969
)
70-
frompandas.core.arrays.string_importStringDtype
7170
frompandas.core.baseimportPandasObject
7271
importpandas.core.commonascom
7372
frompandas.core.indexes.apiimport (
@@ -1218,8 +1217,6 @@ def _format(x):
12181217
returnself.na_rep
12191218
elifisinstance(x,PandasObject):
12201219
returnstr(x)
1221-
elifisinstance(x,StringDtype):
1222-
returnrepr(x)
12231220
else:
12241221
# object dtype
12251222
returnstr(formatter(x))

‎pandas/tests/arrays/string_/test_string.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def test_repr(dtype):
103103
assertrepr(df.A.array)==expected
104104

105105

106+
deftest_dtype_repr(dtype):
107+
ifdtype.storage=="pyarrow":
108+
ifdtype.na_valueispd.NA:
109+
assertrepr(dtype)=="<StringDtype(na_value=<NA>)>"
110+
else:
111+
assertrepr(dtype)=="<StringDtype(na_value=nan)>"
112+
elifdtype.na_valueispd.NA:
113+
assertrepr(dtype)=="<StringDtype(storage='python', na_value=<NA>)>"
114+
else:
115+
assertrepr(dtype)=="<StringDtype(storage='python', na_value=nan)>"
116+
117+
106118
deftest_none_to_nan(cls,dtype):
107119
a=cls._from_sequence(["a",None,"b"],dtype=dtype)
108120
asserta[1]isnotNone

‎pandas/tests/io/formats/test_to_string.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,9 @@ def test_to_string_string_dtype(self):
777777
result=df.dtypes.to_string()
778778
expected=dedent(
779779
"""\
780-
x string[pyarrow]
781-
y string[python]
782-
zint64[pyarrow]"""
780+
xstring
781+
ystring
782+
z int64[pyarrow]"""
783783
)
784784
assertresult==expected
785785

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp