Skip to content

Commit 930d37c

Browse files
committed
ENH: Make print_thing respect display.precision for Real numbers (#60503)
1 parent 5d75d81 commit 930d37c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

pandas/io/formats/printing.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,30 @@ def pprint_thing(
180180
max_seq_items: int | None = None,
181181
) -> str:
182182
"""
183-
This function is the sanctioned way of converting objects
184-
to a string representation and properly handles nested sequences.
183+
Convert object to a string representation, respecting display.precision for Real numbers.
185184
186185
Parameters
187186
----------
188-
thing : anything to be formatted
189-
_nest_lvl : internal use only. pprint_thing() is mutually-recursive
190-
with pprint_sequence, this argument is used to keep track of the
191-
current nesting level, and limit it.
187+
thing : object
188+
Object to be formatted.
189+
_nest_lvl : int, default 0
190+
Internal use only. Current nesting level.
192191
escape_chars : list[str] or Mapping[str, str], optional
193-
Characters to escape. If a Mapping is passed the values are the
194-
replacements
192+
Characters to escape. If a Mapping is passed the values are the replacements.
195193
default_escapes : bool, default False
196-
Whether the input escape characters replaces or adds to the defaults
194+
Whether the input escape characters replaces or adds to the defaults.
195+
quote_strings : bool, default False
196+
Whether to quote strings.
197197
max_seq_items : int or None, default None
198-
Pass through to other pretty printers to limit sequence printing
198+
Pass through to other pretty printers to limit sequence printing.
199199
200200
Returns
201201
-------
202202
str
203+
String representation of the object.
203204
"""
204205

206+
205207
def as_escaped_string(
206208
thing: Any, escape_chars: EscapeChars | None = escape_chars
207209
) -> str:

pandas/tests/io/formats/test_printing.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# functions, not the general printing of pandas objects.
33
from collections.abc import Mapping
44
import string
5-
5+
import numpy as np
66
import pytest
77

8+
from pandas._config.config import option_context # option_context
9+
from pandas.io.formats.printing import pprint_thing
10+
811
import pandas._config.config as cf
912

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

161+
def test_pprint_thing_real_precision(self):
162+
with option_context('display.precision', 3):
163+
assert pprint_thing(3.14159265359) == "3.142"
164+
with option_context('display.precision', 2):
165+
assert pprint_thing(3.14159265359) == "3.14"
166+
167+
158168
def test_ambiguous_width(self):
159169
adj = printing._EastAsianTextAdjustment()
160170
assert adj.len("¡¡ab") == 4

0 commit comments

Comments
 (0)