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

Asymmetry in corner case for DataFrame __getattr__ and __setattr__ #8994

Closed
jakevdp opened this issue Dec 4, 2014 · 2 comments
Closed

Asymmetry in corner case for DataFrame __getattr__ and __setattr__ #8994

jakevdp opened this issue Dec 4, 2014 · 2 comments
Milestone

Comments

@jakevdp
Copy link
Contributor

jakevdp commented Dec 4, 2014

A student of mine ran into a confusing problem which ended up being due to an asymmetry in DataFrame.__setattr__ and DataFrame.__getattr__ when an attribute and a column have the same name. Here is a short example session:

import pandas as pd
print(pd.__version__)  # '0.14.1'
data = pd.DataFrame({'x':[1, 2, 3]})

# try to create a new column, making a common mistake
data.y = 2 * data.x

# oops! That didn't create a column.
# we need to do it this way instead
data['y'] = 2 * data.x

# update the attribute, and it updates the column, not the attribute
data.y = 0
print(data['y'])  # [0, 0, 0]

# print the attribute, and it prints the attribute, not the column
print(data.y)  # [2, 4, 6]
print(data['y']) # [0, 0, 0]

The confusion derived from the fact that in this situation, data.__getattr__('y') refers to the attribute, while data.__setattr__('y', val) refers to the column.

I understand that using attributes to access columns is not recommended, but the asymmetry in this corner case led to a lot of confusion! It would be better if __getattr__ and __setattr__ would always refer to the same object.

@jreback
Copy link
Contributor

jreback commented Dec 4, 2014

related #5904

@jreback jreback added this to the 0.15.2 milestone Dec 4, 2014
@jreback
Copy link
Contributor

jreback commented Dec 7, 2014

closed by #9004

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants