Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert an object of type DataValues.DataValue{Any} to an object of type DateTime #47

Open
IanButterworth opened this issue Aug 11, 2018 · 2 comments

Comments

@IanButterworth
Copy link

I'm not sure if this is a core DataValues issue or one with the implementation in ExcelFiles or DataFrames (where I posted the same issue):
queryverse/ExcelFiles.jl#13
JuliaData/DataFrames.jl#1478

(I'll close the inappropriate issues once I figure out where the issue is)

I have dataframe df where column 1 is an array of DataValues.DataValue{Any}.
How can I convert it to DateTime, and allow for missing?

This doesn’t work:

datetimes = convert(Vector{Union{DateTime,Missing}}, df[1])
MethodError: Cannot `convert` an object of type DataValues.DataValue{Any} to an object of type DateTime
@IanButterworth
Copy link
Author

Here's a concise example for testing:

using DataFrames, Dates, ExcelFiles
filename = "test.xlsx"
df = DataFrame(load(filename, string("Sheet1!A1:E25"),header=true))
df[:DateTime] = convert(Vector{Union{DateTime, Missing}},df[:DateTime])

And source excel file: https://www.dropbox.com/s/cwq5m6m9179s57a/test.xlsx?dl=1

@IanButterworth
Copy link
Author

I've written a temporary function to fix the columns:

function fixcolumn(col,T)
    arr = Vector{Union{Missing, T}}(0)
    for x in col
        try
            push!(arr,x.value)
        catch
            push!(arr,missing)
        end
    end
    return arr
end

For instance:

df[:DateTime] = fixcolumn(df[:DateTime],DateTime)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant