-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
5d75d81
930d37c
6b20acc
9819acf
eb649a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -168,7 +171,6 @@ def _pprint_dict( | |
else: | ||
return fmt.format(things=", ".join(pairs)) | ||
|
||
|
||
def pprint_thing( | ||
thing: object, | ||
_nest_lvl: int = 0, | ||
|
@@ -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ı | ||
if isinstance(thing, Real) and not isinstance(thing, int): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
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 | ||
|
There was a problem hiding this comment.
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.