Skip to content

Commit 5929ae9

Browse files
BUG (string dtype): fix escaping of newline/tab characters in the repr (#60215)
* BUG (string dtype): fix escaping of newline/tab characters in the repr * parametrize existing test for all string dtypes * remove xfail
1 parent 3bcdab2 commit 5929ae9

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

pandas/core/arrays/string_.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from functools import partial
34
import operator
45
from typing import (
56
TYPE_CHECKING,
@@ -64,6 +65,8 @@
6465
from pandas.core.indexers import check_array_indexer
6566
from pandas.core.missing import isna
6667

68+
from pandas.io.formats import printing
69+
6770
if TYPE_CHECKING:
6871
import pyarrow
6972

@@ -391,6 +394,14 @@ def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
391394
raise ValueError
392395
return cls._from_sequence(scalars, dtype=dtype)
393396

397+
def _formatter(self, boxed: bool = False):
398+
formatter = partial(
399+
printing.pprint_thing,
400+
escape_chars=("\t", "\r", "\n"),
401+
quote_strings=not boxed,
402+
)
403+
return formatter
404+
394405
def _str_map(
395406
self,
396407
f,

pandas/tests/frame/test_repr.py

-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import numpy as np
88
import pytest
99

10-
from pandas._config import using_string_dtype
11-
1210
from pandas import (
1311
NA,
1412
Categorical,
@@ -176,7 +174,6 @@ def test_repr_mixed_big(self):
176174

177175
repr(biggie)
178176

179-
@pytest.mark.xfail(using_string_dtype(), reason="/r in")
180177
def test_repr(self):
181178
# columns but no index
182179
no_index = DataFrame(columns=[0, 1, 3])

pandas/tests/series/test_formats.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import numpy as np
77
import pytest
88

9-
from pandas._config import using_string_dtype
10-
119
import pandas as pd
1210
from pandas import (
1311
Categorical,
@@ -143,11 +141,13 @@ def test_tidy_repr_name_0(self, arg):
143141
rep_str = repr(ser)
144142
assert "Name: 0" in rep_str
145143

146-
@pytest.mark.xfail(
147-
using_string_dtype(), reason="TODO(infer_string): investigate failure"
148-
)
149-
def test_newline(self):
150-
ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"])
144+
def test_newline(self, any_string_dtype):
145+
ser = Series(
146+
["a\n\r\tb"],
147+
name="a\n\r\td",
148+
index=Index(["a\n\r\tf"], dtype=any_string_dtype),
149+
dtype=any_string_dtype,
150+
)
151151
assert "\t" not in repr(ser)
152152
assert "\r" not in repr(ser)
153153
assert "a\n" not in repr(ser)

0 commit comments

Comments
 (0)