ENH: Add warning when setting into nonexistent attribute
State: closed

 - [ ] closes #7175
 - [ ] closes #5904
 - [ ] tests added / passed
 - [ ] passes ``git diff upstream/master -u -- "*.py" | flake8 --diff``
 - [ ] whatsnew entry


Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas
name: Compat
description: pandas objects compatability with Numpy or Python functions

Comments:
Hello @deniederhut! Thanks for updating the PR.







Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on August 04, 2017 at 12:16 Hours UTC
Replicate this test from the original issue (and assert each of these things).

```
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)

# this adds a column to df
df['three'] = df.two + 1

# this successfully modifies an existing column
df.one += 1

# this seems to work but does not add a column to df
df.four = df.two + 2

print df.three
print df.four
print df
```

Here is the warning.IOW assignment with a pandas object to a non-existing attribute (actually would be ok with this raising I think), though this *might* break people downstream.
```
# this seems to work but does not add a column to df
df.four = df.two + 2
```
also would love to warn on this: https://github.com/pandas-dev/pandas/issues/5904 as well.
Wilco. Still in `test_generic.py`?
> Wilco. Still in test_generic.py?

yeah that seems reasonable



# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=h1) Report
> Merging [#16951](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/4c498f8451fe4c491a6f38ed9e35da3d3ab6b9b8?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16951/graphs/tree.svg?src=pr&token=eZ4WkYLtcO&width=650&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16951      +/-   ##
==========================================
- Coverage   90.98%   90.96%   -0.02%     
==========================================
  Files         161      161              
  Lines       49288    49290       +2     
==========================================
- Hits        44846    44839       -7     
- Misses       4442     4451       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.74% <100%> (ø)` | :arrow_up: |
| #single | `40.2% <100%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/generic.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ2VuZXJpYy5weQ==) | `92.3% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.76% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=footer). Last update [4c498f8...cfe1bad](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=h1) Report
> Merging [#16951](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/929c66fd74da221078a67ea7fd3dbcbe21d642e0?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16951/graphs/tree.svg?width=650&height=150&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16951      +/-   ##
==========================================
- Coverage   90.99%   90.98%   -0.02%     
==========================================
  Files         162      162              
  Lines       49508    49512       +4     
==========================================
- Hits        45052    45047       -5     
- Misses       4456     4465       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.76% <100%> (ø)` | :arrow_up: |
| #single | `40.27% <40%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/generic.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ2VuZXJpYy5weQ==) | `92.03% <100%> (+0.01%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.66% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=footer). Last update [929c66f...b86546e](https://codecov.io/gh/pandas-dev/pandas/pull/16951?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@deniederhut looks pretty good. rebase on master, some changes and you prob need to force push.
also needs a whatsnew note; put this in the api-breaking changes section. I *think* we may need a full on example to highlite this.
Wilco. 

And I might be missing something, but we are just issuing warnings, right? This shouldn't break any clients unless they are configured to raise warnings as exceptions?
yeah isn't it just a warning then no big deal

however I think we may want to consider making this an error in the future (so can make an issue for that)
Would you like me to add to the whatsnew that these warnings are subject to escalation in future releases?
no but pls open an issue to re-evaluate in the future
Okay, I _think_ I've gotten the docs correct
@jreback I think we might be ready for the final check
thanks @deniederhut nice changes!
@deniederhut 
see https://travis-ci.org/MacPython/pandas-wheels/jobs/262048673

this is a 32-bit python 2.7 build; there are some warnings at the end. See if you can repro (you can try using a 64-bit python 2.7 build)
I have some general comments on this PR (sorry for being late with this feedback):

* I like that you now get a warning when trying to create a new column with `df.col_name = [..]` (which silently fails at creating a new column), that's very useful! However, the case when the name you try to set is a builtin, eg `df.mean = [...]` doesn't raise a warning. 
  I think it would be nice to add a warning then as well (as it is actually a more harmful case, it is not only silently failing, but also overwriting your `mean()` method)

* IMO the warning when setting a new column with `df['new_col'] = ..` which has the same name as a builtin method is a bit overkill. I understand it is confusing from time to time that the attribute access does not always work, but IMO there are many more people who use column names like `mean` without any problem that would now get warnings (one example that this is common is applying functions in groupby operations that create such warnings like `s.groupby(..).agg(['mean', 'median'])`). So I am -1 on this change. 
  See also comment above about `df.mean = ..` not raising a warning while `df['mean] = ..` does, but that's something else as it is actually overwriting the method (and this can also be easily fixed to raise I think). 
  Last, if we keep this warning, I think we should also do it for attributes and not only methods? (eg currently `df['shape']` does not raise a warning)
Happy to dig into this some more - two questions:

1. What is the consensus among the maintainers about warning on name collision?
2. Should this be submitted as a separate PR?
new PR
I also just saw this PR. I agree with @jorisvandenbossche we should not raise warnings when setting column names that conflict with built-in methods. This would be a major source of annoyance for users.

I think we're pretty clear that attribute style access for columns is a convenience feature, not something to be relied on for arbitrary names. In contrast, we *do* guarantee that `__getitem__` works for any valid column name.
@deniederhut do you want to do a PR for this?

@jreback what do you think about the concerns of @shoyer and me? Fine with changing this part of the PR back?
I'll take a look this weekend

On Wed, Aug 16, 2017, 08:13 Joris Van den Bossche <notifications@github.com>
wrote:

> @deniederhut <https://github.com/deniederhut> do you want to do a PR for
> this?
>
> @jreback <https://github.com/jreback> what do you think about the
> concerns of @shoyer <https://github.com/shoyer> and me? Fine with
> changing this part of the PR back?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/16951#issuecomment-322766464>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AGjNrIJP7vd57k2d7VACiSiUdIES21mXks5sYurrgaJpZM4OZEMp>
> .
>

@deniederhut cool! 
I also just opened https://github.com/pandas-dev/pandas/issues/17268 to keep track of this