-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
pytest_warns_bounded, | ||
) | ||
|
||
from pandas.io.formats.format import EngFormatter | ||
from pandas.io.formats.style import Styler | ||
from pandas.io.parsers import TextFileReader | ||
|
||
|
@@ -1698,6 +1699,28 @@ def test_types_to_string() -> None: | |
df.to_string(col_space={"col1": 1, "col2": 3}) | ||
|
||
|
||
def test_dataframe_to_string_float_fmt() -> None: | ||
"""Test the different argument types for float_format.""" | ||
df = pd.DataFrame( | ||
{"values": [2.304, 1.1, 3487392, 13.4732894237, 14.3, 18.0, 17.434, 19.3]} | ||
) | ||
check(assert_type(df.to_string(), str), str) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. you can delete this. It's the same as line 1707 |
||
check(assert_type(df.to_string(float_format=_formatter), str), str) | ||
check( | ||
assert_type( | ||
df.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)), | ||
str, | ||
), | ||
str, | ||
) | ||
check(assert_type(df.to_string(float_format="%.2f"), str), str) | ||
|
||
|
||
def test_types_to_html() -> None: | ||
df = pd.DataFrame( | ||
data={ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,8 @@ | |
) | ||
from tests.extension.decimal.array import DecimalDtype | ||
|
||
from pandas.io.formats.format import EngFormatter | ||
|
||
if TYPE_CHECKING: | ||
from pandas.core.series import ( | ||
OffsetSeries, | ||
|
@@ -2932,6 +2934,29 @@ def test_to_string() -> None: | |
) | ||
|
||
|
||
def test_series_to_string_float_fmt() -> None: | ||
"""Test the different argument types for float_format.""" | ||
sr = pd.Series( | ||
[2.304, 1.1, 3487392, 13.4732894237, 14.3, 18.0, 17.434, 19.3], name="values" | ||
) | ||
check(assert_type(sr.to_string(), str), str) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. delete - same as line 2942 |
||
check(assert_type(sr.to_string(float_format=_formatter), str), str) | ||
check( | ||
assert_type( | ||
sr.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)), | ||
str, | ||
), | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. delete - repeated line |
||
|
||
|
||
def test_types_mask() -> None: | ||
s = pd.Series([1, 2, 3, 4, 5]) | ||
|
||
|
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 anyCallable
.Can you change
FloatFormatType
to useCallable[[float], str]
instead ofCallable
in_typing.pyi
, and then you don't need theCallable
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.