CSV.write() won't take nothing values

Viewed 317

I'm trying to export a Julia dataframe into a CSV and it contains nothing values. To simiplify let's say I have the following dataframe:

using DataFrames, CSV

df = DataFrame(A = [nothing,2,3], B = [4,5,nothing])

When I try to export, I get the following error:

df |> CSV.write("df.csv")

ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.

Should I convert the nothing values to something else like missing? If so, how would I code this?

2 Answers

You can use something function to convert nothing to whatever you want (e.g. missing) like this:

something.(df, missing) |> CSV.write("aaa.txt")

The current reasoning behind the design not to support nothing when writing a CSV file is given here.

I prefer the syntax of the CSV transform argument:

CSV.write("filename.csv", df, transform=(col, val) -> something(val, missing))

From the CSV.write documentation

Posting this as a separate answer, hopefully to distinguish comments and gather clarifications if I am missing something important.

Related