From 5d75d814791d0da0bebebdf0cee49787ac1b1f01 Mon Sep 17 00:00:00 2001 From: aydinomer00 <109145643+aydinomer00@users.noreply.github.com> Date: Sat, 28 Dec 2024 17:34:12 +0300 Subject: [PATCH] ENH: Make print_thing respect display.precision for Real numbers --- pandas/io/formats/printing.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index a9936ba8c8f2c..21de38d2ff236 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -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, @@ -201,19 +203,25 @@ def pprint_thing( """ 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): + 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