Skip to content

Commit 678266c

Browse files
Backport PR #60215 on branch 2.3.x (BUG (string dtype): fix escaping of newline/tab characters in the repr) (#60220)
Backport PR #60215: BUG (string dtype): fix escaping of newline/tab characters in the repr Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent a83184f commit 678266c

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

@@ -387,6 +390,14 @@ def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
387390
raise ValueError
388391
return cls._from_sequence(scalars, dtype=dtype)
389392

393+
def _formatter(self, boxed: bool = False):
394+
formatter = partial(
395+
printing.pprint_thing,
396+
escape_chars=("\t", "\r", "\n"),
397+
quote_strings=not boxed,
398+
)
399+
return formatter
400+
390401
def _str_map(
391402
self,
392403
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,
@@ -144,11 +142,13 @@ def test_tidy_repr_name_0(self, arg):
144142
rep_str = repr(ser)
145143
assert "Name: 0" in rep_str
146144

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

0 commit comments

Comments
 (0)