When saving a Pandas DataFrame to csv, some integers are getting converted in floats.
It happens where a column of floats has missing values (np.nan
).
Is there a simple way to avoid it? (Especially in an automatic way - I often deal with many columns of various data types.)
For example
import pandas as pd
import numpy as np
df = pd.DataFrame([[1,2],[3,np.nan],[5,6]],
columns=["a","b"],
index=["i_1","i_2","i_3"])
df.to_csv("file.csv")
yields
,a,b
i_1,1,2.0
i_2,3,
i_3,5,6.0
What I would like to get is
,a,b
i_1,1,2
i_2,3,
i_3,5,6
EDIT: I am fully aware of Support for integer NA - Pandas Caveats and Gotchas. The question is what is a nice workaround (especially in case if there are many other columns of various types and I do not know in advance which "integer" columns have missing values).