Skip to content

GH730 allow str for float format to string #1200

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

Merged
merged 3 commits into from
Apr 25, 2025

Conversation

loicdiridollou
Copy link
Member

@loicdiridollou loicdiridollou commented Apr 25, 2025

Should fix the issue, yet passing an str to float_format will depend on the underlying type, for example for a float it will work totally fine but for numpy Float64 it won't work.
So the typehints are correct to the extent that it also works at runtime.

Docs are not fixed yet, seems like the issue on the pandas repo is stale pandas-dev/pandas#53675

def _formatter(x) -> str:
return f"{x:.2f}"

check(assert_type(df.to_string(), str), str)
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can delete this. It's the same as line 1707

def _formatter(x) -> str:
return f"{x:.2f}"

check(assert_type(sr.to_string(), str), str)
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete - same as line 2942

str,
)
check(assert_type(sr.to_string(float_format="%.2f"), str), str)
check(assert_type(sr.to_string(float_format="%.2f"), str), str)
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete - repeated line

@@ -2311,7 +2314,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
index: _bool = ...,
na_rep: _str = ...,
formatters: FormattersType | None = ...,
float_format: Callable[[float], str] | None = ...,
float_format: FloatFormatType | Callable[[float], str] | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

FloatFormatType is too wide, as it includes any Callable.

Can you change FloatFormatType to use Callable[[float], str] instead of Callable in _typing.pyi, and then you don't need the Callable declaration here or elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was going to suggest that indeed since we should collapse those two parts.
This is addressed.

@loicdiridollou loicdiridollou force-pushed the gh730_float_format_to_string branch from 2716dd5 to b910bdc Compare April 25, 2025 17:21
@loicdiridollou loicdiridollou requested a review from Dr-Irv April 25, 2025 17:22
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

@Dr-Irv Dr-Irv merged commit ee800a2 into pandas-dev:main Apr 25, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

float_format in .to_string can be str
2 participants