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: How about let pprint_thing print Real instance according to display.precision #60503

Open
3 tasks
miba2020 opened this issue Dec 6, 2024 · 1 comment
Open
3 tasks
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string

Comments

@miba2020
Copy link

miba2020 commented Dec 6, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I use pandas inside ipython. Sometimes I want all float items are printted with 3 digits. But pd.DataFrame({"seq": [[pi] * 5], "foo": [pi]}) got displayed like this:

                                                 seq    foo
0  [3.141592653589793, 3.141592653589793, 3.14159...  3.142

i.e. "foo" in 3 digits but items of "seq" lots digits.

Feature Description

I want pd.DataFrame({"seq": [[pi] * 5], "foo": [pi]}) as

                                   seq    foo
0  [3.142, 3.142, 3.142, 3.142, 3.142]  3.142

in one line and all 5 items shown.

Alternative Solutions

I tried to define a str method for that sequence, but in like this issue customized representation method is not called. So I proposed to patch pprint_thing like this:

diff -u /home/renlifeng/ve3/lib/python3.12/site-packages/pandas/io/formats/printing.py.\~orig\~ /home/renlifeng/ve3/lib/python3.12/site-packages/pandas/io/formats/printing.py
--- /home/renlifeng/ve3/lib/python3.12/site-packages/pandas/io/formats/printing.py.~orig~	2024-10-06 06:11:00.000000000 +0800
+++ /home/renlifeng/ve3/lib/python3.12/site-packages/pandas/io/formats/printing.py	2024-12-04 15:47:37.481159765 +0800
@@ -8,6 +8,10 @@
     Mapping,
     Sequence,
 )
+from numbers import (
+    Integral,
+    Real,
+)
 import sys
 from typing import (
     Any,
@@ -206,7 +210,10 @@
         else:
             escape_chars = escape_chars or ()
 
-        result = str(thing)
+        if isinstance(thing, Real) and not isinstance(thing, Integral):
+            result = f"%.{get_option('display.precision')}f" % thing
+        else:
+            result = str(thing)
         for c in escape_chars:
             result = result.replace(c, translate[c])
         return result

Diff finished.  Wed Dec  4 15:48:27 2024

Additional Context

I use pandas inside ipython. Sometimes I want all float items are printted with 3 digits.

@miba2020 miba2020 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 6, 2024
@rhshadrach
Copy link
Member

Thanks for the report. In theory I'm open to such a change, but pprint_thing is used in over 100 places throughout pandas. We'd need someway to validate that it doesn't cause unexpected undesired behavior.

Alternatively a more targeted change could be made in _pprint_seq. For this, I recommend using pandas' is_float.

@rhshadrach rhshadrach added Output-Formatting __repr__ of pandas objects, to_string and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

2 participants