Skip to content

Commit e63885f

Browse files
authored
DOC: clarify to_csv float format docstring (#47436)
* DOC: clarify to_csv float format docstring * Clarify doc * Clarify doc
1 parent b022a3b commit e63885f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,7 +3302,7 @@ def to_csv(
33023302
path_or_buf: FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None = None,
33033303
sep: str = ",",
33043304
na_rep: str = "",
3305-
float_format: str | None = None,
3305+
float_format: str | Callable | None = None,
33063306
columns: Sequence[Hashable] | None = None,
33073307
header: bool_t | list[str] = True,
33083308
index: bool_t = True,
@@ -3341,8 +3341,9 @@ def to_csv(
33413341
String of length 1. Field delimiter for the output file.
33423342
na_rep : str, default ''
33433343
Missing data representation.
3344-
float_format : str, default None
3345-
Format string for floating point numbers.
3344+
float_format : str, Callable, default None
3345+
Format string for floating point numbers. If a Callable is given, it takes
3346+
precedence over other numeric formatting parameters, like decimal.
33463347
columns : sequence, optional
33473348
Columns to write.
33483349
header : bool or list of str, default True

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,18 @@ def test_to_csv_float_format(self):
833833
)
834834
tm.assert_frame_equal(rs, xp)
835835

836+
def test_to_csv_float_format_over_decimal(self):
837+
# GH#47436
838+
df = DataFrame({"a": [0.5, 1.0]})
839+
result = df.to_csv(
840+
decimal=",",
841+
float_format=lambda x: np.format_float_positional(x, trim="-"),
842+
index=False,
843+
)
844+
expected_rows = ["a", "0.5", "1"]
845+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
846+
assert result == expected
847+
836848
def test_to_csv_unicodewriter_quoting(self):
837849
df = DataFrame({"A": [1, 2, 3], "B": ["foo", "bar", "baz"]})
838850

0 commit comments

Comments
 (0)