-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
GH730 allow str for float format to string #1200
Conversation
tests/test_frame.py
Outdated
def _formatter(x) -> str: | ||
return f"{x:.2f}" | ||
|
||
check(assert_type(df.to_string(), str), str) |
There was a problem hiding this comment.
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
tests/test_series.py
Outdated
def _formatter(x) -> str: | ||
return f"{x:.2f}" | ||
|
||
check(assert_type(sr.to_string(), str), str) |
There was a problem hiding this comment.
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
tests/test_series.py
Outdated
str, | ||
) | ||
check(assert_type(sr.to_string(float_format="%.2f"), str), str) | ||
check(assert_type(sr.to_string(float_format="%.2f"), str), str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete - repeated line
pandas-stubs/core/frame.pyi
Outdated
@@ -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 = ..., |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
2716dd5
to
b910bdc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @loicdiridollou
float_format
in.to_string
can bestr
#730assert_type()
to assert the type of any return valueShould fix the issue, yet passing an
str
tofloat_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