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

sample() from a DataFrame view causes errors #587

Closed
Byrth opened this issue Jul 8, 2020 · 4 comments
Closed

sample() from a DataFrame view causes errors #587

Byrth opened this issue Jul 8, 2020 · 4 comments

Comments

@Byrth
Copy link

Byrth commented Jul 8, 2020

This works as expected:
sample(eachrow(view(df)))

But this fails:
sample(eachrow(view(df)), 10)

and this fails (same cause but different place):
sample(eachrow(view(df)), 10; replace=false)

The errors imply that the x allocated in sample() (line 459 in my version of StatsBase) is allocating a data type that has no implicit conversion:

ERROR: MethodError: Cannot `convert` an object of type DataFrameRow{DataFrame,DataFrames.Index} to an object of type DataFrameRow{SubDataFrame{DataFrame,DataFrames.Index,Array{Int64,1}},DataFrames.Index}
Closest candidates are:
  ...
Stacktrace:
 [1] setindex!(::Array{DataFrameRow{SubDataFrame{DataFrame,DataFrames.Index,Array{Int64,1}},DataFrames.Index},1}, ::DataFrameRow{DataFrame,DataFrames.Index}, ::Int64) at ./array.jl:782
 [2] fisher_yates_sample!(::Random._GLOBAL_RNG, ::DataFrames.DataFrameRows{SubDataFrame{DataFrame,DataFrames.Index,Array{Int64,1}},DataFrames.Index}, ::Array{DataFrameRow{SubDataFrame{DataFrame,DataFrames.Index,Array{Int64,1}},DataFrames.Index},1}) at /root/.julia/packages/StatsBase/unDUx/src/sampling.jl:160

This doesn't affect me because I can just sample 1:nrow(df) and index to get the same row, but just thought I should put it out there.

@nalimilan
Copy link
Member

It works here (after removing view). What versions of Julia, StatsBase and DataFrames are you using?

@Byrth
Copy link
Author

Byrth commented Jul 11, 2020

It worked for me without view too. The error was caused by view.

I was using the max for each at time of posting. I will check and post tomorrow.

@nalimilan
Copy link
Member

view(df) isn't supposed to work, and I don't get the same error as the one you posted.

@Byrth
Copy link
Author

Byrth commented Jul 14, 2020

Ok then

@Byrth Byrth closed this as completed Jul 14, 2020
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

2 participants