Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn a SubDataFrame into a DataFrame?

I have a by which creates SubDataFrames. How do I turn these into a DataFrame, preferably without copying?

My original problem is that I cannot add a new column to a SubDataFrame:

# df[:End] = 1:nrow(merged_islands)
# ERROR: LoadError: Cannot assign to non-existent column: End

# insert!(df, length(df), Array(1:nrow(merged_islands)), :End)
# ERROR: LoadError: MethodError: no method matching insert!(::SubDataFrame{Array{Int64,1}}, ::Int64, ::Array{Int64,1}, ::Symbol)

I am guessing converting it into a DataFrame is the easiest way to do it :)

like image 867
The Unfun Cat Avatar asked Dec 05 '25 12:12

The Unfun Cat


1 Answers

An interesting question. On current master (to be tagged very soon) it is enough to write DataFrame(sdf) where sdf is a SubDataFrame. It will create a copy of all vectors though.

Here is a solution that will create a DataFrame with a view of all vectors contained in SubDataFrame (it should work both on master and on currently tagged release):

function sdf2df(sdf::SubDataFrame)
    p = parent(sdf)
    sel = DataFrames.rows(sdf)
    DataFrame(AbstractVector[view(p[i], sel) for i in 1:ncol(sdf)],
              names(sdf))
end

(I use AbstractVector container type as it will be faster on current master)

You will not be able to add rows to such a DataFrame while it holds at least one view column.

EDIT: as a side note (maybe this was your problem in the end). If you have sdf which is a SubDataFrame whose parent is df which is a DataFrame then if you add columns to df they will be immediately visible in sdf as SubDataFrame only selects rows and inherits all columns from the parent.

like image 72
Bogumił Kamiński Avatar answered Dec 07 '25 15:12

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!