3

I have a dataframe in Python that I want to save as a CSV with this line:

df.to_csv(PATH, quoting = csv.QUOTE_NONNUMERIC, index = False)

The dataframe has this form:

date          timeOfDay    GridID            score
2015-12-31     Morning      1445            0.000000

And the result after that piece of code is:

date,timeOfDay,GridID,score
"2015-01-01","Morning",1445,"0.0"

But I don't understand why score is quoted when the code has csv.QUOTE_NONNUMERIC while it is a numpy.float64. Indeed, GridID is stored successfully.

EDIT: If only score is stored in the csv file and anything else, the values are float, not quoted. Only if I add just a column then score is quoted again.

1 Answer 1

3

There is a bug already on github. So you're actually not doing anything wrong.

Quoting the relevant part from the bug description:

The problem is that pandas.core.internals.FloatBlock.to_native_types (and by extension pandas.formats.format.FloatArrayFormatter.get_result_as_array) unconditionally formats the float array to a str array, which is then passed unchanged to the csv module and hence will be wrapped in quotes by that code.

This is marked as closed and will probably be fixed in a near future release (specifically Next Major Release).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.