-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
ENH: Add warning when setting into nonexistent attribute #16951
Changes from all commits
b5dde1e
4d3f87d
fb29857
af5ab38
f485e77
bfb9f93
61eca9d
15ebdbc
30d98de
8d38f68
c9c43db
0a25a56
150d586
02cb426
562c164
f3fdd19
c6fe062
8ad4d05
e5cc166
e02244d
d38ecca
b86546e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
pandas_dtype) | ||
from pandas.core.dtypes.cast import maybe_promote, maybe_upcast_putmask | ||
from pandas.core.dtypes.missing import isna, notna | ||
from pandas.core.dtypes.generic import ABCSeries, ABCPanel | ||
from pandas.core.dtypes.generic import ABCSeries, ABCPanel, ABCDataFrame | ||
|
||
from pandas.core.common import (_values_from_object, | ||
_maybe_box_datetimelike, | ||
|
@@ -1907,6 +1907,10 @@ def _slice(self, slobj, axis=0, kind=None): | |
return result | ||
|
||
def _set_item(self, key, value): | ||
if isinstance(key, str) and callable(getattr(self, key, None)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use compat.string_types rather than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
warnings.warn("Column name '{key}' collides with a built-in " | ||
"method, which will cause unexpected attribute " | ||
"behavior".format(key=key), stacklevel=3) | ||
self._data.set(key, value) | ||
self._clear_item_cache() | ||
|
||
|
@@ -3357,6 +3361,12 @@ def __setattr__(self, name, value): | |
else: | ||
object.__setattr__(self, name, value) | ||
except (AttributeError, TypeError): | ||
if isinstance(self, ABCDataFrame) and (is_list_like(value)): | ||
warnings.warn("Pandas doesn't allow Series to be assigned " | ||
"into nonexistent columns - see " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would write this a bit more generic, as it does not only raise for Series objects (and I also find it a bit confusing, as pandas certainly allows assigning Series objects, just with another syntax). Maybe something like "pandas doesn't allow to add a new column using attribute access" ? (can certainly be improved further) |
||
"https://pandas.pydata.org/pandas-docs/" | ||
"stable/indexing.html#attribute-access", | ||
stacklevel=2) | ||
object.__setattr__(self, name, value) | ||
|
||
# ---------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a single-point-of-contact that all (most? many?) setter methods go through? i.e. will the various
loc.__setitem__
paths eventually wind through here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe that is correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is my understanding, yes. Here is an example of setting while using
.loc
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this point is only for
[]
setting, e.g. setting a column on a DF or an element on a Series..loc/.iloc
are handled in core/indexing.py