Skip to content

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

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