Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
With the latest version of pandas, fire up a terminal and do the following:
Python 3.11.3 (main, Apr 5 2023, 14:14:40) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.__version__
'2.0.1'
>>> pd.DataFrame([{'a':None, 'b': None}]).to_csv('test-none.csv')
>>> pd.DataFrame([{'a':'', 'b': ''}]).to_csv('test-empty.csv')
Then exit and check the files created:
~ ❯ less test-empty.csv
,a,b
0,,
~ ❯ less test-none.csv
,a,b
0,,
They are the same, when they should not be.
Issue Description
to_csv()
treats None and empty string as the same when they are different.
na_rep
does not solve the issue because it fills all None
s with an empty string. I just want some columns filled with the empty string, and others to be None.
Expected Behavior
The output for test-empty.csv should be:
~ ❯ less test-empty.csv
,a,b
0,"",""
respecting the difference between None and the empty string.
Installed Versions
pandas version '2.0.1'
Comment From: phofl
You can configure the na representation through the keyword na_rep
Comment From: alecstein
You can configure the na representation through the keyword
na_rep
Yes, I mentioned this. Here was my problem with that:
na_rep does not solve the issue because it fills all Nones with an empty string. I just want some columns filled with the empty string, and others to be None.