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