Skip to content

Commit ee800a2

Browse files
GH730 allow str for float format to string (#1200)
* GH730 Add float_format str for to_string methods in Series and DataFrame * GH730 Fix test * GH730 PR feedback
1 parent 66f208f commit ee800a2

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

pandas-stubs/_typing.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ CompressionOptions: TypeAlias = (
626626
FormattersType: TypeAlias = (
627627
list[Callable] | tuple[Callable, ...] | Mapping[str | int, Callable]
628628
)
629-
FloatFormatType: TypeAlias = str | Callable | EngFormatter
629+
FloatFormatType: TypeAlias = str | Callable[[float], str] | EngFormatter
630630
# converters
631631
ConvertersArg: TypeAlias = dict[Hashable, Callable[[Dtype], Dtype]]
632632

pandas-stubs/core/frame.pyi

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ from typing import (
1818
overload,
1919
)
2020

21-
from _typing import TimeZones
21+
from _typing import (
22+
FloatFormatType,
23+
TimeZones,
24+
)
2225
from matplotlib.axes import Axes as PlotAxes
2326
import numpy as np
2427
from pandas import (
@@ -2311,7 +2314,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
23112314
index: _bool = ...,
23122315
na_rep: _str = ...,
23132316
formatters: FormattersType | None = ...,
2314-
float_format: Callable[[float], str] | None = ...,
2317+
float_format: FloatFormatType | None = ...,
23152318
sparsify: _bool | None = ...,
23162319
index_names: _bool = ...,
23172320
justify: _str | None = ...,
@@ -2334,7 +2337,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
23342337
index: _bool = ...,
23352338
na_rep: _str = ...,
23362339
formatters: FormattersType | None = ...,
2337-
float_format: Callable[[float], str] | None = ...,
2340+
float_format: FloatFormatType | None = ...,
23382341
sparsify: _bool | None = ...,
23392342
index_names: _bool = ...,
23402343
justify: _str | None = ...,

pandas-stubs/core/series.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from typing import (
2525
)
2626

2727
from _typing import (
28+
FloatFormatType,
2829
Label,
2930
ReplaceValue,
3031
TimeZones,
@@ -530,7 +531,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
530531
self,
531532
buf: FilePath | WriteBuffer[str],
532533
na_rep: _str = ...,
533-
float_format: Callable[[float], str] = ...,
534+
float_format: FloatFormatType = ...,
534535
header: _bool = ...,
535536
index: _bool = ...,
536537
length: _bool = ...,
@@ -544,7 +545,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
544545
self,
545546
buf: None = ...,
546547
na_rep: _str = ...,
547-
float_format: Callable[[float], str] = ...,
548+
float_format: FloatFormatType = ...,
548549
header: _bool = ...,
549550
index: _bool = ...,
550551
length: _bool = ...,

tests/test_frame.py

+22
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
pytest_warns_bounded,
5656
)
5757

58+
from pandas.io.formats.format import EngFormatter
5859
from pandas.io.formats.style import Styler
5960
from pandas.io.parsers import TextFileReader
6061

@@ -1698,6 +1699,27 @@ def test_types_to_string() -> None:
16981699
df.to_string(col_space={"col1": 1, "col2": 3})
16991700

17001701

1702+
def test_dataframe_to_string_float_fmt() -> None:
1703+
"""Test the different argument types for float_format."""
1704+
df = pd.DataFrame(
1705+
{"values": [2.304, 1.1, 3487392, 13.4732894237, 14.3, 18.0, 17.434, 19.3]}
1706+
)
1707+
check(assert_type(df.to_string(), str), str)
1708+
1709+
def _formatter(x) -> str:
1710+
return f"{x:.2f}"
1711+
1712+
check(assert_type(df.to_string(float_format=_formatter), str), str)
1713+
check(
1714+
assert_type(
1715+
df.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)),
1716+
str,
1717+
),
1718+
str,
1719+
)
1720+
check(assert_type(df.to_string(float_format="%.2f"), str), str)
1721+
1722+
17011723
def test_types_to_html() -> None:
17021724
df = pd.DataFrame(
17031725
data={

tests/test_series.py

+23
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
)
6161
from tests.extension.decimal.array import DecimalDtype
6262

63+
from pandas.io.formats.format import EngFormatter
64+
6365
if TYPE_CHECKING:
6466
from pandas.core.series import (
6567
OffsetSeries,
@@ -2932,6 +2934,27 @@ def test_to_string() -> None:
29322934
)
29332935

29342936

2937+
def test_series_to_string_float_fmt() -> None:
2938+
"""Test the different argument types for float_format."""
2939+
sr = pd.Series(
2940+
[2.304, 1.1, 3487392, 13.4732894237, 14.3, 18.0, 17.434, 19.3], name="values"
2941+
)
2942+
check(assert_type(sr.to_string(), str), str)
2943+
2944+
def _formatter(x) -> str:
2945+
return f"{x:.2f}"
2946+
2947+
check(assert_type(sr.to_string(float_format=_formatter), str), str)
2948+
check(
2949+
assert_type(
2950+
sr.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)),
2951+
str,
2952+
),
2953+
str,
2954+
)
2955+
check(assert_type(sr.to_string(float_format="%.2f"), str), str)
2956+
2957+
29352958
def test_types_mask() -> None:
29362959
s = pd.Series([1, 2, 3, 4, 5])
29372960

0 commit comments

Comments
 (0)