Skip to content

Filtering a copy modifies original DataFrame (and arrays if applicable) #1749

@kescobo

Description

@kescobo

This behavior of filter!() caught me quite off-guard and took a while to debug:

julia> using DataFrames

julia> a = rand(10); b = rand(10); c = rand(10);

julia> df = DataFrame(x = a, y = b, z = c);

julia> df2 = copy(df[[2,3]]);

julia> filter!(row->row[:y] > 0.5, df2);

julia> df
10×3 DataFrame
│ Row │ x         │ y        │ z          │
│     │ Float64   │ Float64  │ Float64    │
├─────┼───────────┼──────────┼────────────┤
│ 10.04035890.9848330.320714   │
│ 20.2213440.9492220.0521154  │
│ 30.2456720.6597390.400802   │
│ 40.4639640.872750.610431   │
│ 50.07977660.8410990.650547   │
│ 60.01938480.9914810.00325831 │
│ 70.929879#undef   │ #undef     │80.611218#undef   │ #undef     │90.509054#undef   │ #undef     │100.381528#undef   │ #undef     │

julia> b
6-element Array{Float64,1}:
 0.9848328699008353
 0.9492215902675882
 0.6597386640528373
 0.8727504349162607
 0.8410991929844753
 0.9914814768586502

I expected filter!() on a DataFrame to only filter out rows on that DataFrame, not to modify the underlying arrays (which are shared across copies). The fix is to use deepcopy() instead.

I'm not sure if this is something that should be "fixed", I think I understand the reasoning behind it, but at the very least I thought I'd open the issue for future stumblers on.

I think this is related to #1695

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