Convert DataFrameRow to Vector

Hi Guys,
I got a super easy question:
when I want to convert a DataFrameRow with the same type to a Vector, I saw from some thread to use:

Vector(data[1,:]) 
#or
convert(Vector, data[1,:])

but some times I got the error message that:
MethodError: Cannot convert an object of type DataFrameRow{DataFrame, DataFrames.Index} to an object of type Vector{Int64}
PS: I finally end up using collect(values(data[1:])) where the values function would return a NTuple and use collect to make it a vector

Could any one tell me the best way or correct way to do it ?

You were close. Usually convert and the constructor are used for things which as “very alike”. A DataFrameRow isn’t close enough to qualify for that I guess. You want

collect(data[1, :])
1 Like

Thanks! learned two new ways to use collect today, such a magic function

This should work always. Can you please share the example allowing to reproduce the problem since if it fails it is a bug. Thank you!

1 Like

I have opened https://github.com/JuliaData/DataFrames.jl/issues/3017 to track this so you can also comment there with your failing example directly. Thank you!

1 Like

Sorry for the late reply, I thought it was solved. :frowning:
I encountered the issue from the course’s problem set 1: mitmath/6S083: Materials for MIT 6.S083 / 18.S190: Computational thinking with Julia + application to the COVID-19 pandemic (github.com) which is offered by Julia academy.
And I try to run the error code but it seems to work now, the error was shown in a for-loop when I convert the DataFrameRow to a vector, so I tested it separately, and Vector(data[1,:]) seems to work, and I could not figure out why so I post this problem and used sometimes to describe it. After I switch to use collect(data[1,:]) there is no error anymore, so I didn’t think too much.
So I think maybe there is no bug in the source code, it could probably be my problem. So sorry for that, maybe you should close the issue. Big thanks for always giving the most effective solutions that help me a lot in the journey to learn Julia lang.

1 Like

Thank you for reporting. I will investigate the code you have linked and make sure we do not have a bug :).

1 Like