Skip to content

ENH: Expose integer formatting also for default display, not only styler #57177

Open
@enryH

Description

@enryH

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I noticed that in Pandas version 2.2 the internal implementation of IntArrayFormatter was changed (or at least renamed), marking the classes as private using one underscore. I used the implementation as described here to format integers in a DataFrame on each display.

See changes between 2.1 and 2.2 to make the linked patch work: link

I see that the styler supports formatting thousands:

import pandas as pd
pd.options.styler.format.thousands = ','
s = pd.Series([1_000_000]).to_frame()
s.style # does change display of s to 1,000,000

however

s # will display 1000000

Would it be possible to add the same to the display options?

Feature Description

pd.options.format.thousands = ','

would need to pass:

if pd.options.format.thousands:
  format_str = f':{pd.options.format.thousands}d'.format
else:
  format_str = ':d'.format

Alternative Solutions

Change the default here

class IntArrayFormatter(pf._GenericArrayFormatter):

    def _format_strings(self):
        formatter = self.formatter or '{:,d}'.format
        fmt_values = [formatter(x) for x in self.values]
        return fmt_values

pf._IntArrayFormatter = IntArrayFormatter

Using the above patch I get the display I would prefer:

>>> pd.Series([1_000_000])
0    1,000,000
dtype: int64

Additional Context

Working with large integers in tables is made easier if thousands are visually distinguished. Maybe there is already a way to specify that formatter function (str.format) for display, which would be perfect.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions