From 0418b6f4eacd0aff8a82bdf2ef7251d920c49922 Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 20 Jun 2022 22:22:44 +0200 Subject: [PATCH 1/3] DOC: clarify to_csv float format docstring --- pandas/core/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f32b347de32c3..dcb4e438e6e5a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3342,7 +3342,8 @@ def to_csv( na_rep : str, default '' Missing data representation. float_format : str, default None - Format string for floating point numbers. + Format string for floating point numbers. If a format is given, it takes + precedence over other numeric formatting parameters, like decimal. columns : sequence, optional Columns to write. header : bool or list of str, default True From 5f8d37c2f38247ff1ee01ebf3952971cd0a2633a Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Tue, 21 Jun 2022 22:05:22 +0200 Subject: [PATCH 2/3] Clarify doc --- pandas/core/generic.py | 6 +++--- pandas/tests/frame/methods/test_to_csv.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index dcb4e438e6e5a..a9fe349d53e4d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3302,7 +3302,7 @@ def to_csv( path_or_buf: FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None = None, sep: str = ",", na_rep: str = "", - float_format: str | None = None, + float_format: str | Callable | None = None, columns: Sequence[Hashable] | None = None, header: bool_t | list[str] = True, index: bool_t = True, @@ -3341,8 +3341,8 @@ def to_csv( String of length 1. Field delimiter for the output file. na_rep : str, default '' Missing data representation. - float_format : str, default None - Format string for floating point numbers. If a format is given, it takes + float_format : str, Callable, default None + Format string for floating point numbers. If a Callable is given, it takes precedence over other numeric formatting parameters, like decimal. columns : sequence, optional Columns to write. diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index f1ecad2f711bc..7695291950d0c 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -833,6 +833,14 @@ def test_to_csv_float_format(self): ) tm.assert_frame_equal(rs, xp) + def test_to_csv_float_format_over_decimal(self): + # GH#47436 + df = DataFrame({"a": [0.5, 1.0]}) + result = df.to_csv(decimal=",", float_format=lambda x: np.format_float_positional(x, trim="-"), index=False) + expected_rows = ["a", "0.5", "1"] + expected = tm.convert_rows_list_to_csv_str(expected_rows) + assert result == expected + def test_to_csv_unicodewriter_quoting(self): df = DataFrame({"A": [1, 2, 3], "B": ["foo", "bar", "baz"]}) From ee6c7fac48fcc9c6ddba34c721a3fe34ef44e600 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Tue, 21 Jun 2022 22:09:36 +0200 Subject: [PATCH 3/3] Clarify doc --- pandas/tests/frame/methods/test_to_csv.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 7695291950d0c..df7bc04202e39 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -836,7 +836,11 @@ def test_to_csv_float_format(self): def test_to_csv_float_format_over_decimal(self): # GH#47436 df = DataFrame({"a": [0.5, 1.0]}) - result = df.to_csv(decimal=",", float_format=lambda x: np.format_float_positional(x, trim="-"), index=False) + result = df.to_csv( + decimal=",", + float_format=lambda x: np.format_float_positional(x, trim="-"), + index=False, + ) expected_rows = ["a", "0.5", "1"] expected = tm.convert_rows_list_to_csv_str(expected_rows) assert result == expected