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: pprint_thing(..., quote_strings=True) fails for strings with embedded single-quotes #60190

Open
3 tasks done
zpincus opened this issue Nov 5, 2024 · 3 comments · May be fixed by #60251
Open
3 tasks done

BUG: pprint_thing(..., quote_strings=True) fails for strings with embedded single-quotes #60190

zpincus opened this issue Nov 5, 2024 · 3 comments · May be fixed by #60251
Assignees
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string

Comments

@zpincus
Copy link

zpincus commented Nov 5, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

from pandas.io.formats.printing import pprint_thing

print(pprint_thing("'510", quote_strings=True))

Issue Description

The code above produces the incorrect ''510' as output, instead of "'510" as python does, or even '\'510'.

In the current master the cause is clear enough:

    elif isinstance(thing, str) and quote_strings:
        result = f"'{as_escaped_string(thing)}'"

Either python's string repr could be used (assuming the rest of its behavior is fit for the purpose), or single-quotes could be explicitly backslash-escaped.

Expected Behavior

As above.

Installed Versions

As above, but is present in GitHub master as of right now.

@zpincus zpincus added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 5, 2024
@rhshadrach
Copy link
Member

Thanks for the report. While this method is not public, the issue can manifest in e.g.

df = pd.DataFrame({"'a": [1, 1, 2], "b": [3, 4, 5], "c": 6}).set_index(["'a", "b"])
print(df.index.names)
# [''a', 'b']

PRs to fix are welcome!

@rhshadrach rhshadrach added Output-Formatting __repr__ of pandas objects, to_string and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 5, 2024
@thedataninja1786
Copy link

take

@thedataninja1786
Copy link

thedataninja1786 commented Nov 7, 2024

This can be addressed by modifying __str__ in ..\core\indexes\frozen.py to include single quotes also def __str__(self) -> str: return pprint_thing(self, quote_strings=True, escape_chars=("\t", "\r", "\n","'")) and extending pprint_thing's to translate = {"\t": r"\t", "\n": r"\n", "\r": r"\r", "'":""} in ..\printing.py to remove them. I believe escaping single quotes does not work here since the output itself is wrapped in single quotes.

@thedataninja1786 thedataninja1786 linked a pull request Nov 8, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants