Skip to content

Commit 8349816

Browse files
GH730 Add float_format str for to_string methods in Series and DataFrame
1 parent 66f208f commit 8349816

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 6 additions & 3 deletions
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 | Callable[[float], str] | 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 | Callable[[float], str] | None = ...,
23382341
sparsify: _bool | None = ...,
23392342
index_names: _bool = ...,
23402343
justify: _str | None = ...,

pandas-stubs/core/series.pyi

Lines changed: 3 additions & 2 deletions
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 | Callable[[float], str] = ...,
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 | Callable[[float], str] = ...,
548549
header: _bool = ...,
549550
index: _bool = ...,
550551
length: _bool = ...,

tests/test_frame.py

Lines changed: 23 additions & 0 deletions
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,28 @@ 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(), str), str)
1713+
check(assert_type(df.to_string(float_format=_formatter), str), str)
1714+
check(
1715+
assert_type(
1716+
df.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)),
1717+
str,
1718+
),
1719+
str,
1720+
)
1721+
check(assert_type(df.to_string(float_format=".2%"), str), str)
1722+
1723+
17011724
def test_types_to_html() -> None:
17021725
df = pd.DataFrame(
17031726
data={

tests/test_series.py

Lines changed: 24 additions & 0 deletions
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,28 @@ 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(), str), str)
2948+
check(assert_type(sr.to_string(float_format=_formatter), str), str)
2949+
check(
2950+
assert_type(
2951+
sr.to_string(float_format=EngFormatter(accuracy=2, use_eng_prefix=False)),
2952+
str,
2953+
),
2954+
str,
2955+
)
2956+
check(assert_type(sr.to_string(float_format=".2%"), str), str)
2957+
2958+
29352959
def test_types_mask() -> None:
29362960
s = pd.Series([1, 2, 3, 4, 5])
29372961

0 commit comments

Comments
 (0)