Skip to content

Commit 8db43ed

Browse files
authored
BUG: to_csv not respecting float_format for Float64 (#46007)
1 parent 43ca51f commit 8db43ed

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ I/O
353353
- Bug in :func:`read_excel` results in an infinite loop with certain ``skiprows`` callables (:issue:`45585`)
354354
- Bug in :meth:`DataFrame.info` where a new line at the end of the output is omitted when called on an empty :class:`DataFrame` (:issue:`45494`)
355355
- Bug in :func:`read_csv` not recognizing line break for ``on_bad_lines="warn"`` for ``engine="c"`` (:issue:`41710`)
356+
- Bug in :meth:`DataFrame.to_csv` not respecting ``float_format`` for ``Float64`` dtype (:issue:`45991`)
356357
- Bug in :func:`read_parquet` when ``engine="pyarrow"`` which caused partial write to disk when column of unsupported datatype was passed (:issue:`44914`)
357358
- Bug in :func:`DataFrame.to_excel` and :class:`ExcelWriter` would raise when writing an empty DataFrame to a ``.ods`` file (:issue:`45793`)
358359

pandas/core/internals/blocks.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
is_dtype_equal,
5252
is_interval_dtype,
5353
is_list_like,
54+
is_sparse,
5455
is_string_dtype,
5556
)
5657
from pandas.core.dtypes.dtypes import (
@@ -2257,14 +2258,7 @@ def to_native_types(
22572258
results_converted.append(result.astype(object, copy=False))
22582259
return np.vstack(results_converted)
22592260

2260-
elif isinstance(values, ExtensionArray):
2261-
mask = isna(values)
2262-
2263-
new_values = np.asarray(values.astype(object))
2264-
new_values[mask] = na_rep
2265-
return new_values
2266-
2267-
elif values.dtype.kind == "f":
2261+
elif values.dtype.kind == "f" and not is_sparse(values):
22682262
# see GH#13418: no special formatting is desired at the
22692263
# output (important for appropriate 'quoting' behaviour),
22702264
# so do not pass it through the FloatArrayFormatter
@@ -2294,6 +2288,13 @@ def to_native_types(
22942288
res = res.astype(object, copy=False)
22952289
return res
22962290

2291+
elif isinstance(values, ExtensionArray):
2292+
mask = isna(values)
2293+
2294+
new_values = np.asarray(values.astype(object))
2295+
new_values[mask] = na_rep
2296+
return new_values
2297+
22972298
else:
22982299

22992300
mask = isna(values)

pandas/tests/io/formats/test_to_csv.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,32 @@ def test_to_csv_date_format_in_categorical(self):
315315
ser = ser.astype("category")
316316
assert ser.to_csv(index=False, date_format="%Y-%m-%d") == expected
317317

318+
def test_to_csv_float_ea_float_format(self):
319+
# GH#45991
320+
df = DataFrame({"a": [1.1, 2.02, pd.NA, 6.000006], "b": "c"})
321+
df["a"] = df["a"].astype("Float64")
322+
result = df.to_csv(index=False, float_format="%.5f")
323+
expected = """a,b
324+
1.10000,c
325+
2.02000,c
326+
,c
327+
6.00001,c
328+
"""
329+
assert result == expected
330+
331+
def test_to_csv_float_ea_no_float_format(self):
332+
# GH#45991
333+
df = DataFrame({"a": [1.1, 2.02, pd.NA, 6.000006], "b": "c"})
334+
df["a"] = df["a"].astype("Float64")
335+
result = df.to_csv(index=False)
336+
expected = """a,b
337+
1.1,c
338+
2.02,c
339+
,c
340+
6.000006,c
341+
"""
342+
assert result == expected
343+
318344
def test_to_csv_multi_index(self):
319345
# see gh-6618
320346
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))

0 commit comments

Comments
 (0)