Skip to content

Unexpected behavior during vector assignment: df[0] = df[0] + 10 #496

@gioele

Description

@gioele

DataFrame assignments behave in a surprising way then numerical indexes are used: instead of the vector replacing the old one, they are added to the dataframe. This is in contrast with what happen when explicit names are used.

Take for example this dataframe:

df = Daru::DataFrame.new({ :a => [1,2,3,4], :b => [5,6,7,8] })
=> #<Daru::DataFrame(4x2)>
       a   b
   0   1   5
   1   2   6
   2   3   7
   3   4   8

Assigning df[0] will add a new vector to the dataframe instead of replacing the 0th column:

df[0] = df[0] + 10; df
=> #<Daru::DataFrame(4x3)>
       a   b   0
   0   1   5  11
   1   2   6  12
   2   3   7  13
   3   4   8  14

This is surprising, considered that assigning to df[:a] replaces the :a column as expected:

 df[:a] = df[:a] + 10; df
=> #<Daru::DataFrame(4x2)>
       a   b
   0  11   5
   1  12   6
   2  13   7
   3  14   8

and that df[:a] and df[0] both return the same vector

df[:a]
=> #<Daru::Vector(4)>
       a
   0  1
   1  2
   2  3
   3  4
df[0]
=> #<Daru::Vector(4)>
       a
   0  1
   1  2
   2  3
   3  4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions