Skip to content

Commit c04b874

Browse files
committed
TST: Adds testing around warnings for setting attributes
1 parent 0673d7f commit c04b874

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

pandas/tests/dtypes/test_generic.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
import pandas as pd
66
from pandas.core.dtypes import generic as gt
7-
import pytest
7+
from pandas.util import testing as tm
88

99

1010
class TestABCClasses(object):
@@ -40,8 +40,33 @@ def test_abc_types(self):
4040
assert isinstance(self.sparse_array, gt.ABCSparseArray)
4141
assert isinstance(self.categorical, gt.ABCCategorical)
4242
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
4363
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

Comments
 (0)