Skip to content

Commit

Permalink
BUG (string dtype): fix escaping of newline/tab characters in the repr (
Browse files Browse the repository at this point in the history
#60215)

* BUG (string dtype): fix escaping of newline/tab characters in the repr

* parametrize existing test for all string dtypes

* remove xfail
  • Loading branch information
jorisvandenbossche authored Nov 6, 2024
1 parent 3bcdab2 commit 5929ae9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
11 changes: 11 additions & 0 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from functools import partial
import operator
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -64,6 +65,8 @@
from pandas.core.indexers import check_array_indexer
from pandas.core.missing import isna

from pandas.io.formats import printing

if TYPE_CHECKING:
import pyarrow

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

def _formatter(self, boxed: bool = False):
formatter = partial(
printing.pprint_thing,
escape_chars=("\t", "\r", "\n"),
quote_strings=not boxed,
)
return formatter

def _str_map(
self,
f,
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/frame/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

from pandas import (
NA,
Categorical,
Expand Down Expand Up @@ -176,7 +174,6 @@ def test_repr_mixed_big(self):

repr(biggie)

@pytest.mark.xfail(using_string_dtype(), reason="/r in")
def test_repr(self):
# columns but no index
no_index = DataFrame(columns=[0, 1, 3])
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/series/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

import pandas as pd
from pandas import (
Categorical,
Expand Down Expand Up @@ -143,11 +141,13 @@ def test_tidy_repr_name_0(self, arg):
rep_str = repr(ser)
assert "Name: 0" in rep_str

@pytest.mark.xfail(
using_string_dtype(), reason="TODO(infer_string): investigate failure"
)
def test_newline(self):
ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"])
def test_newline(self, any_string_dtype):
ser = Series(
["a\n\r\tb"],
name="a\n\r\td",
index=Index(["a\n\r\tf"], dtype=any_string_dtype),
dtype=any_string_dtype,
)
assert "\t" not in repr(ser)
assert "\r" not in repr(ser)
assert "a\n" not in repr(ser)
Expand Down

0 comments on commit 5929ae9

Please sign in to comment.