Skip to content

Commit

Permalink
BUG: escape single quotes in index names when printing (#60251)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedataninja1786 authored Nov 29, 2024
1 parent 1d809c3 commit a4fc97e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
-
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)

Missing
^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def _disabled(self, *args, **kwargs) -> NoReturn:
raise TypeError(f"'{type(self).__name__}' does not support mutable operations.")

def __str__(self) -> str:
return pprint_thing(self, quote_strings=True, escape_chars=("\t", "\r", "\n"))
return pprint_thing(
self, quote_strings=True, escape_chars=("\t", "\r", "\n", "'")
)

def __repr__(self) -> str:
return f"{type(self).__name__}({self!s})"
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def pprint_thing(
def as_escaped_string(
thing: Any, escape_chars: EscapeChars | None = escape_chars
) -> str:
translate = {"\t": r"\t", "\n": r"\n", "\r": r"\r"}
translate = {"\t": r"\t", "\n": r"\n", "\r": r"\r", "'": r"\'"}
if isinstance(escape_chars, Mapping):
if default_escapes:
translate.update(escape_chars)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@
from collections.abc import Mapping
import string

import pytest

import pandas._config.config as cf

import pandas as pd

from pandas.io.formats import printing


@pytest.mark.parametrize(
"input_names, expected_names",
[
(["'a b"], "['\\'a b']"), # Escape leading quote
(["test's b"], "['test\\'s b']"), # Escape apostrophe
(["'test' b"], "['\\'test\\' b']"), # Escape surrounding quotes
(["test b'"], "['test b\\'']"), # Escape single quote
(["test\n' b"], "['test\\n\\' b']"), # Escape quotes, preserve newline
],
)
def test_formatted_index_names(input_names, expected_names):
# GH#60190
df = pd.DataFrame({name: [1, 2, 3] for name in input_names}).set_index(input_names)
formatted_names = str(df.index.names)

assert formatted_names == expected_names


def test_adjoin():
data = [["a", "b", "c"], ["dd", "ee", "ff"], ["ggg", "hhh", "iii"]]
expected = "a dd ggg\nb ee hhh\nc ff iii"
Expand Down

0 comments on commit a4fc97e

Please sign in to comment.