Skip to content

Feature Request: Allow new column setting via attribute assignment #11848

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

Closed
nickeubank opened this issue Dec 15, 2015 · 1 comment
Closed

Feature Request: Allow new column setting via attribute assignment #11848

nickeubank opened this issue Dec 15, 2015 · 1 comment
Labels
API Design Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@nickeubank
Copy link
Contributor

One "gotcha" in pandas that's always bugged me is that you can generally access and change columns in a DataFrame by dot-messaging (attribute assignment) except for adding new columns. For example, one can play with existing columns easily:

df = pd.DataFrame({'col1':range(10,14),
                   'col2':range(20,24)})

df.col1 = 3
df
Out[5]: 
   col1  col2
0     3    20
1     3    21
2     3    22
3     3    23

But you can't set new columns:

df.col3 = pd.Series(range(30,40))
df

Out[6]: 

   col1  col2
0     3    20
1     3    21
2     3    22
3     3    23

Seems like it wouldn't be hard to modify __setattr__ so that if the setting value is a Series, NumPy Array, or scalar value, just call df[key] = value.

Seem reasonable?

(Still wrestling with copy-on-write PR, but happy to come back to this if others support and no on addresses till then)

@jreback
Copy link
Contributor

jreback commented Dec 15, 2015

dupe of #9033

but this would be a monumental API change and break pretty much the python standard of arbitrary setting of attributes.

@jreback jreback closed this as completed Dec 15, 2015
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design Duplicate Report Duplicate issue or pull request labels Dec 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants