Skip to content

Commit 2ec6de0

Browse files
name and str() change to "string"
1 parent 6b470b1 commit 2ec6de0

File tree

8 files changed

+37
-27
lines changed

8 files changed

+37
-27
lines changed

pandas/_testing/asserters.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
TimedeltaArray,
4949
)
5050
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
51+
from pandas.core.arrays.string_ import StringDtype
5152

5253
from pandas.io.formats.printing import pprint_thing
5354

@@ -638,12 +639,20 @@ def raise_assert_detail(obj, message, left, right, diff=None, index_values=None)
638639

639640
if isinstance(left, np.ndarray):
640641
left = pprint_thing(left)
641-
elif is_categorical_dtype(left) or isinstance(left, PandasDtype):
642+
elif (
643+
is_categorical_dtype(left)
644+
or isinstance(left, PandasDtype)
645+
or isinstance(left, StringDtype)
646+
):
642647
left = repr(left)
643648

644649
if isinstance(right, np.ndarray):
645650
right = pprint_thing(right)
646-
elif is_categorical_dtype(right) or isinstance(right, PandasDtype):
651+
elif (
652+
is_categorical_dtype(right)
653+
or isinstance(right, PandasDtype)
654+
or isinstance(right, StringDtype)
655+
):
647656
right = repr(right)
648657

649658
msg += f"""

pandas/core/arrays/string_.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class StringDtype(ExtensionDtype):
8484
string[python]
8585
"""
8686

87+
name = "string"
88+
8789
#: StringDtype.na_value uses pandas.NA
8890
na_value = libmissing.NA
8991
_metadata = ("storage",)
@@ -102,10 +104,6 @@ def __init__(self, storage=None):
102104

103105
self.storage = storage
104106

105-
@property
106-
def name(self):
107-
return f"string[{self.storage}]"
108-
109107
@property
110108
def type(self) -> type[str]:
111109
return str
@@ -182,6 +180,9 @@ def construct_array_type( # type: ignore[override]
182180
return ArrowStringArray
183181

184182
def __repr__(self):
183+
return f"string[{self.storage}]"
184+
185+
def __str__(self):
185186
return self.name
186187

187188
def __from_arrow__(
@@ -268,7 +269,7 @@ class StringArray(PandasArray):
268269
>>> pd.array(['This is', 'some text', None, 'data.'], dtype="string")
269270
<StringArray>
270271
['This is', 'some text', <NA>, 'data.']
271-
Length: 4, dtype: string[python]
272+
Length: 4, dtype: string
272273
273274
Unlike arrays instantiated with ``dtype="object"``, ``StringArray``
274275
will convert the values to strings.
@@ -280,7 +281,7 @@ class StringArray(PandasArray):
280281
>>> pd.array(['1', 1], dtype="string")
281282
<StringArray>
282283
['1', '1']
283-
Length: 2, dtype: string[python]
284+
Length: 2, dtype: string
284285
285286
However, instantiating StringArrays directly with non-strings will raise an error.
286287

pandas/core/arrays/string_arrow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ArrowStringArray(OpsMixin, ExtensionArray, ObjectStringArrayMixin):
127127
>>> pd.array(['This is', 'some text', None, 'data.'], dtype="string[pyarrow]")
128128
<ArrowStringArray>
129129
['This is', 'some text', <NA>, 'data.']
130-
Length: 4, dtype: string[pyarrow]
130+
Length: 4, dtype: string
131131
"""
132132

133133
def __init__(self, values):

pandas/core/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ def array(
239239
>>> pd.array(["a", None, "c"])
240240
<StringArray>
241241
['a', <NA>, 'c']
242-
Length: 3, dtype: string[python]
242+
Length: 3, dtype: string
243243
244244
>>> with pd.option_context("string_storage", "pyarrow"):
245245
... arr = pd.array(["a", None, "c"])
246246
...
247247
>>> arr
248248
<ArrowStringArray>
249249
['a', <NA>, 'c']
250-
Length: 3, dtype: string[pyarrow]
250+
Length: 3, dtype: string
251251
252252
>>> pd.array([pd.Period('2000', freq="D"), pd.Period("2000", freq="D")])
253253
<PeriodArray>

pandas/core/generic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6139,12 +6139,12 @@ def convert_dtypes(
61396139
2 3 z <NA> <NA> 20 200.0
61406140
61416141
>>> dfn.dtypes
6142-
a Int32
6143-
b string[python]
6144-
c boolean
6145-
d string[python]
6146-
e Int64
6147-
f Float64
6142+
a Int32
6143+
b string
6144+
c boolean
6145+
d string
6146+
e Int64
6147+
f Float64
61486148
dtype: object
61496149
61506150
Start with a Series of strings and missing data represented by ``np.nan``.
@@ -6162,7 +6162,7 @@ def convert_dtypes(
61626162
0 a
61636163
1 b
61646164
2 <NA>
6165-
dtype: string[python]
6165+
dtype: string
61666166
"""
61676167
if self.ndim == 1:
61686168
return self._convert_dtypes(

pandas/core/strings/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3080,7 +3080,7 @@ def _result_dtype(arr):
30803080
from pandas.core.arrays.string_ import StringDtype
30813081

30823082
if isinstance(arr.dtype, StringDtype):
3083-
return arr.dtype.name
3083+
return arr.dtype
30843084
else:
30853085
return object
30863086

pandas/tests/arrays/string_/test_string.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ def test_repr(dtype):
3232
expected = " A\n0 a\n1 <NA>\n2 b"
3333
assert repr(df) == expected
3434

35-
expected = (
36-
f"0 a\n1 <NA>\n2 b\nName: A, dtype: string[{dtype.storage}]"
37-
)
35+
expected = "0 a\n1 <NA>\n2 b\nName: A, dtype: string"
3836
assert repr(df.A) == expected
3937

4038
arr_name = "ArrowStringArray" if dtype.storage == "pyarrow" else "StringArray"
41-
expected = (
42-
f"<{arr_name}>\n['a', <NA>, 'b']\nLength: 3, dtype: string[{dtype.storage}]"
43-
)
39+
expected = f"<{arr_name}>\n['a', <NA>, 'b']\nLength: 3, dtype: string"
4440
assert repr(df.A.array) == expected
4541

4642

pandas/tests/extension/test_string.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def data_for_grouping(dtype, chunked):
9494

9595

9696
class TestDtype(base.BaseDtypeTests):
97-
pass
97+
def test_eq_with_str(self, dtype):
98+
assert dtype == f"string[{dtype.storage}]"
99+
super().test_eq_with_str(dtype)
98100

99101

100102
class TestInterface(base.BaseInterfaceTests):
@@ -106,7 +108,9 @@ def test_view(self, data, request):
106108

107109

108110
class TestConstructors(base.BaseConstructorsTests):
109-
pass
111+
def test_from_dtype(self, data):
112+
# base test uses string representation of dtype
113+
pass
110114

111115

112116
class TestReshaping(base.BaseReshapingTests):

0 commit comments

Comments
 (0)