Overwrite partial DataFrame Row with new Array

I’m attempting to overwrite the last two elements in a dataframe row with the contents of an array.

df = DataFrame([1 2 3])
df[1,2:3] = DataFrame([5 10])
ERROR: BoundsError: attempt to access "attempt to access a data frame with 2 columns at index 1"

If I attempt to overwrite the first two elements (instead of the last two), there are no errors.

df[1,1:2] = DataFrame([5 10])

I believe the first example used to work on earlier versions.

Also, is there any difference between these two techniques for converting an array to a dataframe?

array = [1 2 3]
convert(DataFrame, array)
DataFrame(array)

are equivalent.

will not be allowed, because LHS is 1-dimensional (DataFrameRow), and RHS is 2-dimensional (DataFrame).

In the 0.20 release of DataFrames.jl you will be able to put any iterable object to a DataFrameRow via setindex!. For now, what you can do is use broadcasting assignment:

df[1,2:3] .= [5, 10]

CC @nalimilan - the inconsistency @milesf notices is due to the fact that DataFrame has length defined. That is why I want to end deprecation period in https://github.com/JuliaData/DataFrames.jl/pull/1899 PR (as discussed in https://github.com/JuliaData/DataFrames.jl/pull/1899#discussion_r317367048 coment).

Writing on behalf of @bkamins, as he has no Internet access now.

After the analysis (he was using a development branch in tests of your code). The code

df[1,2:3] = DataFrame([5 10])

should not be used (it is deprecated), but it should work correctly on 0.19 version. The reason why it fails is a bug in deprecation code. He will submit a PR to fix this.

1 Like

See https://github.com/JuliaData/DataFrames.jl/pull/1928. You can use the fix I have used there already now if you really need to use 1-row DataFrame on RHS (but I would not recommend doing this as the support for this will be soon removed).