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: Make print_thing respect display.precision for Real numbers #60613

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aydinomer00
Copy link

  • closes ENH: How about let pprint_thing print Real instance according to display.precision #60503

  • Tests added and passed:

    • Added test_pprint_thing_real_precision to test precision formatting
    • Verified with multiple display.precision values (2, 3, 4)
    • Tested with various float numbers including np.pi
  • All code checks passed

  • Added type annotations for clarity in modified functions

  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file:
    - Enhanced print_thing to respect display.precision setting for Real numbers

This PR modifies the print_thing function to respect display.precision settings when formatting Real numbers. For example:

with pd.option_context('display.precision', 2):
    df = pd.DataFrame({"value": [np.pi]})
    print(df)  # Shows 3.14

with pd.option_context('display.precision', 4):
    print(df)  # Shows 3.1416

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use is_float from padnas.core.dtypes.inference

escape_chars = list(escape_chars.keys())
else:
escape_chars = escape_chars or ()

result = str(thing)
# Real instance kontrolü ve precision uygulaması
Copy link
Member

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.

import pytest

from pandas._config.config import option_context # option_context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the comment

@@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move to TestPPrintThing above

@@ -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):
with option_context('display.precision', 3):
assert pprint_thing(3.14159265359) == "3.142"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use printing.pprint_thing instead, as the other tests here do

@rhshadrach rhshadrach added Output-Formatting __repr__ of pandas objects, to_string Enhancement labels Dec 30, 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

Successfully merging this pull request may close these issues.

ENH: How about let pprint_thing print Real instance according to display.precision
2 participants