Imagine I have a DataFrame with some of the columns being Unitful. Is there a way to save such a DataFrame into a file while preserving the information about the units?
The only solution I can think about is to strip it off units and save it to a .csv file. Then, when I load the file, I will have to multiply the columns by units.
But may be there is a good established approach to this?
I remember trying various serialization solutions and found Serialization to work best in terms of speed and serializing custom structs. Plus its a standard lib, which is a plus
There is a package for reversible converting : UnitfulParsableString.jl
I’m using these functions for Arrows saving using that package:
function deunitful(df::AbstractDataFrame)
for colname in names(df)
col = df[!,colname]
firstval = skipmissing(col)[1]
if firstval isa Number
ustr = string(Unitful.unit(firstval))
df[!,colname] .= ustrip.(df[!,colname])
colmetadata!(df, colname, "units", ustr, style=:note);
colmetadata(df, colname, "units")
end
end
df
end
function reunitful(df::AbstractDataFrame)
for colname in names(df)
if "units" in colmetadatakeys(df, colname)
ustr = colmetadata(df, colname, "units")
un = Unitful.uparse(ustr)
df[!,colname] .= df[!,colname] .* un
deletecolmetadata!(df, colname, "units")
end
end
df
end