Skip to content

Commit 619d2bb

Browse files
committed
split bool ops test, parametrize where possible
1 parent af5e8ec commit 619d2bb

File tree

1 file changed

+128
-85
lines changed

1 file changed

+128
-85
lines changed

pandas/tests/series/test_operators.py

+128-85
Original file line numberDiff line numberDiff line change
@@ -183,46 +183,47 @@ def test_comparison_tuples(self):
183183
expected = Series([True, False])
184184
assert_series_equal(result, expected)
185185

186-
def test_comparison_operators_with_nas(self):
186+
@pytest.mark.parametrize('op', ['lt', 'le', 'gt', 'ge', 'eq', 'ne'])
187+
def test_comparison_operators_with_nas(self, op):
187188
ser = Series(bdate_range('1/1/2000', periods=10), dtype=object)
188189
ser[::2] = np.nan
189190

190191
# test that comparisons work
191-
ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne']
192-
for op in ops:
193-
val = ser[5]
192+
val = ser[5]
194193

195-
f = getattr(operator, op)
196-
result = f(ser, val)
194+
f = getattr(operator, op)
195+
result = f(ser, val)
197196

198-
expected = f(ser.dropna(), val).reindex(ser.index)
197+
expected = f(ser.dropna(), val).reindex(ser.index)
199198

200-
if op == 'ne':
201-
expected = expected.fillna(True).astype(bool)
202-
else:
203-
expected = expected.fillna(False).astype(bool)
199+
if op == 'ne':
200+
expected = expected.fillna(True).astype(bool)
201+
else:
202+
expected = expected.fillna(False).astype(bool)
204203

205-
assert_series_equal(result, expected)
204+
assert_series_equal(result, expected)
206205

207-
# fffffffuuuuuuuuuuuu
208-
# result = f(val, s)
209-
# expected = f(val, s.dropna()).reindex(s.index)
210-
# assert_series_equal(result, expected)
206+
# fffffffuuuuuuuuuuuu
207+
# result = f(val, s)
208+
# expected = f(val, s.dropna()).reindex(s.index)
209+
# assert_series_equal(result, expected)
211210

212-
# boolean &, |, ^ should work with object arrays and propagate NAs
211+
@pytest.mark.parametrize('bool_op', ['and_', 'or_', 'xor'])
212+
def test_bool_operators_with_nas(self, bool_op):
213+
# boolean &, |, ^ should work with object arrays and propagate NAs
213214

214-
ops = ['and_', 'or_', 'xor']
215-
mask = ser.isna()
216-
for bool_op in ops:
217-
func = getattr(operator, bool_op)
215+
ser = Series(bdate_range('1/1/2000', periods=10), dtype=object)
216+
ser[::2] = np.nan
218217

219-
filled = ser.fillna(ser[0])
218+
func = getattr(operator, bool_op)
219+
result = func(ser < ser[9], ser > ser[3])
220220

221-
result = func(ser < ser[9], ser > ser[3])
221+
mask = ser.isna()
222+
filled = ser.fillna(ser[0])
223+
expected = func(filled < filled[9], filled > filled[3])
224+
expected[mask] = False
222225

223-
expected = func(filled < filled[9], filled > filled[3])
224-
expected[mask] = False
225-
assert_series_equal(result, expected)
226+
assert_series_equal(result, expected)
226227

227228
def test_comparison_object_numeric_nas(self):
228229
ser = Series(np.random.randn(10), dtype=object)
@@ -376,10 +377,10 @@ def test_comparison_different_length(self):
376377
b = Series([2, 3, 4])
377378
pytest.raises(ValueError, a.__eq__, b)
378379

379-
def test_comparison_label_based(self):
380+
def test_bool_opslabel_based(self):
380381

381382
# GH 4947
382-
# comparisons should be label based
383+
# boolean ops should be label based
383384

384385
a = Series([True, False, True], list('bca'))
385386
b = Series([False, True, False], list('abc'))
@@ -1823,128 +1824,169 @@ def test_ops_datetimelike_align(self):
18231824

18241825
def test_operators_bitwise(self):
18251826
# GH 9016: support bitwise op for integer types
1826-
index = list('bca')
1827+
s_0123 = Series(range(4), dtype='int64')
1828+
s_1111 = Series([1] * 4, dtype='int8')
18271829

1828-
s_tft = Series([True, False, True], index=index)
1829-
s_fff = Series([False, False, False], index=index)
1830-
s_tff = Series([True, False, False], index=index)
1831-
s_empty = Series([])
1830+
# We cannot wrap s111.astype(np.int32) with pd.Index because it
1831+
# will case to int64
1832+
res = s_0123.astype(np.int16) | s_1111.astype(np.int32)
1833+
expected = Series([1, 1, 3, 3], dtype='int32')
1834+
assert_series_equal(res, expected)
18321835

1833-
# TODO: unused
1834-
# s_0101 = Series([0, 1, 0, 1])
1836+
def test_operators_bitwise_invalid(self):
1837+
# GH#9016: support bitwise op for integer types
1838+
s_0123 = Series(range(4), dtype='int64')
1839+
s_1111 = Series([1] * 4, dtype='int8')
1840+
1841+
with pytest.raises(TypeError):
1842+
s_1111 & 'a'
1843+
with pytest.raises(TypeError):
1844+
s_1111 & ['a', 'b', 'c', 'd']
1845+
with pytest.raises(TypeError):
1846+
s_0123 & 3.14
1847+
with pytest.raises(TypeError):
1848+
s_0123 & [0.1, 4, 3.14, 2]
18351849

1850+
def test_operators_bitwise_int_series_with_float_series(self):
1851+
# GH#9016: support bitwise op for integer types
1852+
s_0123 = Series(range(4), dtype='int64')
1853+
s_ftft = Series([False, True, False, True])
1854+
result = s_0123 & Series([0.1, 4, -3.14, 2])
1855+
assert_series_equal(result, s_ftft)
1856+
1857+
@pytest.mark.parametrize('box', [np.array, pd.Index, pd.Series])
1858+
def test_operators_bitwise_with_int_arraylike(self, box):
1859+
# GH#9016: support bitwise op for integer types
1860+
# GH#??? allow for operating with Index
18361861
s_0123 = Series(range(4), dtype='int64')
18371862
s_3333 = Series([3] * 4)
18381863
s_4444 = Series([4] * 4)
18391864

1840-
res = s_tft & s_empty
1841-
expected = s_fff
1865+
res = s_0123 & box(s_3333)
1866+
expected = Series(range(4), dtype='int64')
18421867
assert_series_equal(res, expected)
18431868

1844-
res = s_tft | s_empty
1845-
expected = s_tft
1869+
res = s_0123 | box(s_4444)
1870+
expected = Series(range(4, 8), dtype='int64')
18461871
assert_series_equal(res, expected)
18471872

1848-
res = s_0123 & s_3333
1849-
expected = Series(range(4), dtype='int64')
1873+
s_1111 = Series([1] * 4, dtype='int8')
1874+
res = s_0123 & box(s_1111)
1875+
expected = Series([0, 1, 0, 1], dtype='int64')
18501876
assert_series_equal(res, expected)
18511877

1852-
res = s_0123 | s_4444
1853-
expected = Series(range(4, 8), dtype='int64')
1854-
assert_series_equal(res, expected)
1878+
def test_operators_bitwise_with_bool(self):
1879+
# GH#9016: support bitwise op for integer types
1880+
s_0123 = Series(range(4), dtype='int64')
18551881

1856-
s_a0b1c0 = Series([1], list('b'))
1882+
assert_series_equal(s_0123 & False, Series([False] * 4))
1883+
assert_series_equal(s_0123 ^ False, Series([False, True, True, True]))
1884+
assert_series_equal(s_0123 & [False], Series([False] * 4))
1885+
assert_series_equal(s_0123 & (False), Series([False] * 4))
18571886

1858-
res = s_tft & s_a0b1c0
1859-
expected = s_tff.reindex(list('abc'))
1860-
assert_series_equal(res, expected)
1887+
def test_operators_bitwise_with_integers(self):
1888+
# GH#9016: support bitwise op for integer types
1889+
index = list('bca')
18611890

1862-
res = s_tft | s_a0b1c0
1863-
expected = s_tft.reindex(list('abc'))
1864-
assert_series_equal(res, expected)
1891+
s_tft = Series([True, False, True], index=index)
1892+
s_fff = Series([False, False, False], index=index)
1893+
s_0123 = Series(range(4), dtype='int64')
18651894

1866-
n0 = 0
1867-
res = s_tft & n0
1895+
res = s_tft & 0
18681896
expected = s_fff
18691897
assert_series_equal(res, expected)
18701898

1871-
res = s_0123 & n0
1899+
res = s_0123 & 0
18721900
expected = Series([0] * 4)
18731901
assert_series_equal(res, expected)
18741902

1875-
n1 = 1
1876-
res = s_tft & n1
1903+
res = s_tft & 1
18771904
expected = s_tft
18781905
assert_series_equal(res, expected)
18791906

1880-
res = s_0123 & n1
1907+
res = s_0123 & 1
18811908
expected = Series([0, 1, 0, 1])
18821909
assert_series_equal(res, expected)
18831910

1884-
s_1111 = Series([1] * 4, dtype='int8')
1885-
res = s_0123 & s_1111
1886-
expected = Series([0, 1, 0, 1], dtype='int64')
1911+
def test_operators_bitwise_with_reindexing(self):
1912+
# ops where reindeing needs to be done, so operating with a Series
1913+
# makes sense but with an Index or array does not
1914+
# GH#9016: support bitwise op for integer types
1915+
index = list('bca')
1916+
1917+
s_tft = Series([True, False, True], index=index)
1918+
s_fff = Series([False, False, False], index=index)
1919+
s_tff = Series([True, False, False], index=index)
1920+
s_empty = Series([])
1921+
s_a0b1c0 = Series([1], list('b'))
1922+
s_0123 = Series(range(4), dtype='int64')
1923+
1924+
res = s_tft & s_empty
1925+
expected = s_fff
18871926
assert_series_equal(res, expected)
18881927

1889-
res = s_0123.astype(np.int16) | s_1111.astype(np.int32)
1890-
expected = Series([1, 1, 3, 3], dtype='int32')
1928+
res = s_tft | s_empty
1929+
expected = s_tft
1930+
assert_series_equal(res, expected)
1931+
1932+
res = s_tft & s_a0b1c0
1933+
expected = s_tff.reindex(list('abc'))
18911934
assert_series_equal(res, expected)
18921935

1893-
pytest.raises(TypeError, lambda: s_1111 & 'a')
1894-
pytest.raises(TypeError, lambda: s_1111 & ['a', 'b', 'c', 'd'])
1895-
pytest.raises(TypeError, lambda: s_0123 & np.NaN)
1896-
pytest.raises(TypeError, lambda: s_0123 & 3.14)
1897-
pytest.raises(TypeError, lambda: s_0123 & [0.1, 4, 3.14, 2])
1936+
res = s_tft | s_a0b1c0
1937+
expected = s_tft.reindex(list('abc'))
1938+
assert_series_equal(res, expected)
18981939

18991940
# s_0123 will be all false now because of reindexing like s_tft
19001941
if compat.PY3:
19011942
# unable to sort incompatible object via .union.
19021943
exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3])
19031944
with tm.assert_produces_warning(RuntimeWarning):
1904-
assert_series_equal(s_tft & s_0123, exp)
1945+
result = s_tft & s_0123
1946+
assert_series_equal(result, exp)
19051947
else:
19061948
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
1907-
assert_series_equal(s_tft & s_0123, exp)
1949+
result = s_tft & s_0123
1950+
assert_series_equal(result, exp)
19081951

19091952
# s_tft will be all false now because of reindexing like s_0123
19101953
if compat.PY3:
19111954
# unable to sort incompatible object via .union.
19121955
exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a'])
19131956
with tm.assert_produces_warning(RuntimeWarning):
1957+
result = s_0123 & s_tft
19141958
assert_series_equal(s_0123 & s_tft, exp)
19151959
else:
19161960
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
19171961
assert_series_equal(s_0123 & s_tft, exp)
19181962

1919-
assert_series_equal(s_0123 & False, Series([False] * 4))
1920-
assert_series_equal(s_0123 ^ False, Series([False, True, True, True]))
1921-
assert_series_equal(s_0123 & [False], Series([False] * 4))
1922-
assert_series_equal(s_0123 & (False), Series([False] * 4))
1923-
assert_series_equal(s_0123 & Series([False, np.NaN, False, False]),
1924-
Series([False] * 4))
1963+
def test_operators_bitwise_with_nans(self):
1964+
# with NaNs present op(series, series) works but op(series, index)
1965+
# is not implemented
1966+
# GH#9016: support bitwise op for integer types
1967+
s_0123 = Series(range(4), dtype='int64')
1968+
result = s_0123 & Series([False, np.NaN, False, False])
1969+
assert_series_equal(result, Series([False] * 4))
19251970

19261971
s_ftft = Series([False, True, False, True])
1927-
assert_series_equal(s_0123 & Series([0.1, 4, -3.14, 2]), s_ftft)
1928-
19291972
s_abNd = Series(['a', 'b', np.NaN, 'd'])
1930-
res = s_0123 & s_abNd
1973+
result = s_0123 & s_abNd
19311974
expected = s_ftft
1932-
assert_series_equal(res, expected)
1975+
assert_series_equal(result, expected)
19331976

19341977
def test_scalar_na_cmp_corners(self):
19351978
s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10])
19361979

1937-
def tester(a, b):
1938-
return a & b
1939-
1940-
pytest.raises(TypeError, tester, s, datetime(2005, 1, 1))
1980+
with pytest.raises(TypeError):
1981+
s & datetime(2005, 1, 1)
19411982

19421983
s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)])
19431984
s[::2] = np.nan
19441985

19451986
expected = Series(True, index=s.index)
19461987
expected[::2] = False
1947-
assert_series_equal(tester(s, list(s)), expected)
1988+
result = s & list(s)
1989+
assert_series_equal(result, expected)
19481990

19491991
d = DataFrame({'A': s})
19501992
# TODO: Fix this exception - needs to be fixed! (see GH5035)
@@ -1955,7 +1997,8 @@ def tester(a, b):
19551997
# https://github.com/pandas-dev/pandas/issues/5284
19561998

19571999
pytest.raises(ValueError, lambda: d.__and__(s, axis='columns'))
1958-
pytest.raises(ValueError, tester, s, d)
2000+
with pytest.raises(ValueError):
2001+
s & d
19592002

19602003
# this is wrong as its not a boolean result
19612004
# result = d.__and__(s,axis='index')

0 commit comments

Comments
 (0)