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

leftjoin! is actually copying reference instead of value?! #3379

Closed
mzy2240 opened this issue Sep 13, 2023 · 1 comment
Closed

leftjoin! is actually copying reference instead of value?! #3379

mzy2240 opened this issue Sep 13, 2023 · 1 comment

Comments

@mzy2240
Copy link

mzy2240 commented Sep 13, 2023

MWE:

a = DataFrame(x=[1,1])
b = DataFrame(x=[1], y=[[1,2,3]])
leftjoin!(a, b, on=:x)
deleteat!(a[1,:y], 1)
println(a)

And out of surprise, you would see that both vectors in the y column got updated ([1,2,3] -> [2,3])! If I iterate over each row using eachrow, then changing the first row would also affect the second row. I don't know whether it is a desired behavior (I am sure in Pandas it does not behave like this), and I am wondering what to do if I only want to change the first vector in the y column.

Thanks!

@mzy2240 mzy2240 changed the title leftjoin! use reference instead of data?! leftjoin! is actually copying reference instead of value?! Sep 13, 2023
@bkamins
Copy link
Member

bkamins commented Sep 14, 2023

I am sure in Pandas it does not behave like this

Pandas behaves the same way:

>>> df1 = pd.DataFrame({'key':[1,1]})
>>> df2 = pd.DataFrame({'key':[1], 'value':[[1,2,3]]})
>>> res = pd.merge(df1, df2, how='left', left_on='key',right_on='key')
>>> res['value']
0    [1, 2, 3]
1    [1, 2, 3]
Name: value, dtype: object
>>> res['value'][0].append(4)
>>> res['value']
0    [1, 2, 3, 4]
1    [1, 2, 3, 4]
Name: value, dtype: object
>>> df2['value']
0    [1, 2, 3, 4]
Name: value, dtype: object

if you want a copy you have to do it manually after a join by doing a.y = copy.(a.y) or a.y = deepcopy(a.y).

@bkamins bkamins closed this as completed Sep 14, 2023
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