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 60503 print precision #1

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

from pandas._config import get_option

from pandas.core.dtypes.inference import is_sequence
from pandas.core.dtypes.inference import is_sequence, is_float

from pandas.io.formats.console import get_console_size

Expand Down Expand Up @@ -168,7 +168,6 @@ def _pprint_dict(
else:
return fmt.format(things=", ".join(pairs))


def pprint_thing(
thing: object,
_nest_lvl: int = 0,
Expand All @@ -178,42 +177,46 @@ 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.

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)
if is_float(thing):
result = f"{thing:.{get_option('display.precision')}f}"
else:
result = str(thing)

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

import pytest

import pandas._config.config as cf
from pandas._config.config import option_context
from pandas.io.formats import printing

import pandas._config.config as cf
import pandas as pd

from pandas.io.formats import printing


@pytest.mark.parametrize(
"input_names, expected_names",
Expand Down Expand Up @@ -82,6 +81,13 @@ def test_repr_dict(self):
def test_repr_mapping(self):
assert printing.pprint_thing(MyMapping()) == "{'a': 4, 'b': 4}"

def test_pprint_thing_real_precision(self):
from pandas.io.formats.printing import pprint_thing
with option_context('display.precision', 3):
assert pprint_thing(3.14159265359) == "3.142"
with option_context('display.precision', 2):
assert pprint_thing(3.14159265359) == "3.14"


class TestFormatBase:
def test_adjoin(self):
Expand Down