I'm effectively trying to read the following file into a DataFrame and write it out again, unchanged.
F1,F2,F3,F4,F5
"blah","blah",123,"123","123"
"blue","blue",456,"456",""
I would have expected that pandas.read_csv would interpret all fields in the above as strings (and thus dtype:object
) except for F3.
However, F4 is being treated as a numeric field and given dtype:int64
This means that writing the data back again with quoting=csv.QUOTE_NONNUMERIC
loses the quotes around F4.
pd.read_csv(test, quotechar='"', sep=',')
Out[90]:
F1 F2 F3 F4 F5
0 blah blah 123 123 123.0
1 blue blue 456 456 NaN
pd.read_csv("test.txt", quotechar='"', sep=',').dtypes
Out[91]:
F1 object
F2 object
F3 int64
F4 int64
dtype: object
This also creates a further problem: if any of the F5 values are an empty string (i.e. ""
), F5 is instead treated as float64
with a NaN
value for the empty strings. Those values will be written back as ""
, but all the actual values will now have a .0
appended as they're considered floats.
I've played around with various options for the quoting
parameter - quoting=csv.QUOTE_NONE
is the only one which treats F4 and F5 as a string, but then I obviously end up with the actual quotation marks embedded in the data. I would have expected quoting=csv.QUOTE_NONNUMERIC
to do what I want, but it treats all of F3, F4, F5 as float64
.
I feel like I'm maybe missing something fundamental here about how dtypes are determined?
The only workaround I can think of is to read the header and first data line of every file, parse it for quoted fields, and build an explicit dict on the fly (or read the file first with quoting=csv.QUOTE_NONE
and do the same thing based on which columns contain quote marks) then read the file again with the explicit dtypes.
This seems like a long-winded (and slow!) alternative, so I'm hoping someone can point out something I've missed in the read_csv documentation. Thanks.
Extra detail in case anything is relevant:
- I'm processing a large number of files each with a large (and different) set of columns, so I can't specify explicit dtypes for each one.
- Data to be treated as text is in double quotes, remaining data is numeric.
- Obviously I'm not leaving the data completely unchanged, but columns other than the ones I'm adding/amending should be unaffected.
- The files are well-formed CSVs for the most part, no extraneous spaces between separators
- Most have some extra header lines which I'm parsing first and skipping with
header=nnn
when I call theread_csv
.