Skip to content

pandas.DataFrame.to_csv float_format parameter not working as expected with pandas.Float64Dtype dtype #45991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rlshuhart opened this issue Feb 14, 2022 · 1 comment · Fixed by #46007
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@rlshuhart
Copy link

rlshuhart commented Feb 14, 2022

Reproducible Example

import pandas as pd
print(f"pd.__version__ = {pd.__version__}")

df = pd.DataFrame({'a':[1.1, 2.02, 3.003, 4.0004, None, 6.000006], 'b':['a', 'b', 'c', 'd', 'e', 'f']})
df.to_csv('test_float_format_no_convert_dtypes.csv', index=False, float_format='%.5f')

df_converted = df.convert_dtypes()
df_converted.to_csv('test_float_format_yes_convert_dtypes.csv', index=False, float_format='%.5f')

When np.float64 / expected result:

a,b
1.10000,a
2.02000,b
3.00300,c
4.00040,d
,e
6.00001,f

When pd.Float64Dtype (row 6 should be 6.00001):

a,b
1.1,a
2.02,b
3.003,c
4.0004,d
,e
6.000006,f
@jreback jreback added ExtensionArray Extending pandas with custom dtypes or arrays. IO CSV read_csv, to_csv labels Feb 16, 2022
@jreback jreback added this to the 1.5 milestone Feb 16, 2022
@jreback jreback added the Output-Formatting __repr__ of pandas objects, to_string label Feb 16, 2022
@rlshuhart
Copy link
Author

Thanks @phofl!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants