|
4 | 4 | import numpy as np
|
5 | 5 | import pandas as pd
|
6 | 6 | from pandas.core.dtypes import generic as gt
|
| 7 | +from pandas.util import testing as tm |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class TestABCClasses(object):
|
@@ -38,3 +39,40 @@ def test_abc_types(self):
|
38 | 39 | assert isinstance(self.sparse_array, gt.ABCSparseArray)
|
39 | 40 | assert isinstance(self.categorical, gt.ABCCategorical)
|
40 | 41 | assert isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCPeriod)
|
| 42 | + |
| 43 | + |
| 44 | +def test_setattr_warnings(): |
| 45 | + # GH5904 - Suggestion: Warning for DataFrame colname-methodname clash |
| 46 | + # GH7175 - GOTCHA: You can't use dot notation to add a column... |
| 47 | + d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']), |
| 48 | + 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])} |
| 49 | + df = pd.DataFrame(d) |
| 50 | + |
| 51 | + with catch_warnings(record=True) as w: |
| 52 | + # successfully add new column |
| 53 | + # this should not raise a warning |
| 54 | + df['three'] = df.two + 1 |
| 55 | + assert len(w) == 0 |
| 56 | + assert df.three.sum() > df.two.sum() |
| 57 | + |
| 58 | + with catch_warnings(record=True) as w: |
| 59 | + # successfully modify column in place |
| 60 | + # this should not raise a warning |
| 61 | + df.one += 1 |
| 62 | + assert len(w) == 0 |
| 63 | + assert df.one.iloc[0] == 2 |
| 64 | + |
| 65 | + with catch_warnings(record=True) as w: |
| 66 | + # successfully add an attribute to a series |
| 67 | + # this should not raise a warning |
| 68 | + df.two.not_an_index = [1, 2] |
| 69 | + assert len(w) == 0 |
| 70 | + |
| 71 | + with tm.assert_produces_warning(UserWarning): |
| 72 | + # warn when setting column to nonexistent name |
| 73 | + df.four = df.two + 2 |
| 74 | + assert df.four.sum() > df.two.sum() |
| 75 | + |
| 76 | + with tm.assert_produces_warning(UserWarning): |
| 77 | + # warn when column has same name as method |
| 78 | + df['sum'] = df.two |
0 commit comments