Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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