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

ENH: Make print_thing respect display.precision for Real numbers #60613

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
38 changes: 24 additions & 14 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

from pandas._config import get_option

from numbers import Real # Real
from pandas._config import get_option # display.precision

from pandas.core.dtypes.inference import is_sequence

from pandas.io.formats.console import get_console_size
Expand Down Expand Up @@ -168,7 +171,6 @@ def _pprint_dict(
else:
return fmt.format(things=", ".join(pairs))


def pprint_thing(
thing: object,
_nest_lvl: int = 0,
Expand All @@ -178,42 +180,50 @@ def pprint_thing(
max_seq_items: int | None = None,
) -> str:
"""
This function is the sanctioned way of converting objects
to a string representation and properly handles nested sequences.
Convert object to a string representation, respecting display.precision for Real numbers.

Parameters
----------
thing : anything to be formatted
_nest_lvl : internal use only. pprint_thing() is mutually-recursive
with pprint_sequence, this argument is used to keep track of the
current nesting level, and limit it.
thing : object
Object to be formatted.
_nest_lvl : int, default 0
Internal use only. Current nesting level.
escape_chars : list[str] or Mapping[str, str], optional
Characters to escape. If a Mapping is passed the values are the
replacements
Characters to escape. If a Mapping is passed the values are the replacements.
default_escapes : bool, default False
Whether the input escape characters replaces or adds to the defaults
Whether the input escape characters replaces or adds to the defaults.
quote_strings : bool, default False
Whether to quote strings.
max_seq_items : int or None, default None
Pass through to other pretty printers to limit sequence printing
Pass through to other pretty printers to limit sequence printing.

Returns
-------
str
String representation of the object.
"""


def as_escaped_string(
thing: Any, escape_chars: EscapeChars | None = escape_chars
thing: Any, escape_chars: EscapeChars | None = escape_chars
) -> str:
translate = {"\t": r"\t", "\n": r"\n", "\r": r"\r", "'": r"\'"}
if isinstance(escape_chars, Mapping):
if default_escapes:
translate.update(escape_chars)
else:
translate = escape_chars # type: ignore[assignment]
translate = escape_chars
escape_chars = list(escape_chars.keys())
else:
escape_chars = escape_chars or ()

result = str(thing)
# Real instance kontrolü ve precision uygulaması
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment repeats the code verbatim and can be removed. Also, can you make any comments in English.

if isinstance(thing, Real) and not isinstance(thing, int):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use is_float from padnas.core.dtypes.inference

precision = get_option("display.precision")
result = f"{thing:.{precision}f}"
else:
result = str(thing)

for c in escape_chars:
result = result.replace(c, translate[c])
return result
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
# functions, not the general printing of pandas objects.
from collections.abc import Mapping
import string

import numpy as np
import pytest

from pandas._config.config import option_context # option_context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the comment

from pandas.io.formats.printing import pprint_thing

import pandas._config.config as cf

import pandas as pd
Expand Down Expand Up @@ -155,6 +158,13 @@ def test_east_asian_len(self):
assert adj.len("パンダpanda") == 11
assert adj.len("パンダpanda") == 10

def test_pprint_thing_real_precision(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move to TestPPrintThing above

with option_context('display.precision', 3):
assert pprint_thing(3.14159265359) == "3.142"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use printing.pprint_thing instead, as the other tests here do

with option_context('display.precision', 2):
assert pprint_thing(3.14159265359) == "3.14"


def test_ambiguous_width(self):
adj = printing._EastAsianTextAdjustment()
assert adj.len("¡¡ab") == 4
Expand Down
Loading