Skip to content

Commit a22acc2

Browse files
jbrockmendeljreback
authored andcommitted
centralize and split frame division tests (pandas-dev#19527)
1 parent 672f5a1 commit a22acc2

File tree

3 files changed

+121
-80
lines changed

3 files changed

+121
-80
lines changed

pandas/tests/frame/test_arithmetic.py

+121-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -*- coding: utf-8 -*-
2-
32
import pytest
43
import numpy as np
54

5+
from pandas.compat import range
6+
67
import pandas as pd
78
import pandas.util.testing as tm
89

@@ -58,10 +59,129 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
5859
result = getattr(empty, opname)(const).get_dtype_counts()
5960
tm.assert_series_equal(result, pd.Series([2], ['bool']))
6061

62+
@pytest.mark.parametrize('timestamps', [
63+
[pd.Timestamp('2012-01-01 13:00:00+00:00')] * 2,
64+
[pd.Timestamp('2012-01-01 13:00:00')] * 2])
65+
def test_tz_aware_scalar_comparison(self, timestamps):
66+
# Test for issue #15966
67+
df = pd.DataFrame({'test': timestamps})
68+
expected = pd.DataFrame({'test': [False, False]})
69+
tm.assert_frame_equal(df == -1, expected)
70+
6171

6272
# -------------------------------------------------------------------
6373
# Arithmetic
6474

75+
class TestFrameMulDiv(object):
76+
"""Tests for DataFrame multiplication and division"""
77+
# ------------------------------------------------------------------
78+
# Mod By Zero
79+
80+
def test_df_mod_zero_df(self):
81+
# GH#3590, modulo as ints
82+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
83+
84+
# this is technically wrong, as the integer portion is coerced to float
85+
# ###
86+
first = pd.Series([0, 0, 0, 0], dtype='float64')
87+
second = pd.Series([np.nan, np.nan, np.nan, 0])
88+
expected = pd.DataFrame({'first': first, 'second': second})
89+
result = df % df
90+
tm.assert_frame_equal(result, expected)
91+
92+
def test_df_mod_zero_array(self):
93+
# GH#3590, modulo as ints
94+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
95+
96+
# this is technically wrong, as the integer portion is coerced to float
97+
# ###
98+
first = pd.Series([0, 0, 0, 0], dtype='float64')
99+
second = pd.Series([np.nan, np.nan, np.nan, 0])
100+
expected = pd.DataFrame({'first': first, 'second': second})
101+
102+
# numpy has a slightly different (wrong) treatment
103+
with np.errstate(all='ignore'):
104+
arr = df.values % df.values
105+
result2 = pd.DataFrame(arr, index=df.index,
106+
columns=df.columns, dtype='float64')
107+
result2.iloc[0:3, 1] = np.nan
108+
tm.assert_frame_equal(result2, expected)
109+
110+
def test_df_mod_zero_int(self):
111+
# GH#3590, modulo as ints
112+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
113+
114+
result = df % 0
115+
expected = pd.DataFrame(np.nan, index=df.index, columns=df.columns)
116+
tm.assert_frame_equal(result, expected)
117+
118+
# numpy has a slightly different (wrong) treatment
119+
with np.errstate(all='ignore'):
120+
arr = df.values.astype('float64') % 0
121+
result2 = pd.DataFrame(arr, index=df.index, columns=df.columns)
122+
tm.assert_frame_equal(result2, expected)
123+
124+
def test_df_mod_zero_series_does_not_commute(self):
125+
# GH#3590, modulo as ints
126+
# not commutative with series
127+
df = pd.DataFrame(np.random.randn(10, 5))
128+
ser = df[0]
129+
res = ser % df
130+
res2 = df % ser
131+
assert not res.fillna(0).equals(res2.fillna(0))
132+
133+
# ------------------------------------------------------------------
134+
# Division By Zero
135+
136+
def test_df_div_zero_df(self):
137+
# integer div, but deal with the 0's (GH#9144)
138+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
139+
result = df / df
140+
141+
first = pd.Series([1.0, 1.0, 1.0, 1.0])
142+
second = pd.Series([np.nan, np.nan, np.nan, 1])
143+
expected = pd.DataFrame({'first': first, 'second': second})
144+
tm.assert_frame_equal(result, expected)
145+
146+
def test_df_div_zero_array(self):
147+
# integer div, but deal with the 0's (GH#9144)
148+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
149+
150+
first = pd.Series([1.0, 1.0, 1.0, 1.0])
151+
second = pd.Series([np.nan, np.nan, np.nan, 1])
152+
expected = pd.DataFrame({'first': first, 'second': second})
153+
154+
with np.errstate(all='ignore'):
155+
arr = df.values.astype('float') / df.values
156+
result = pd.DataFrame(arr, index=df.index,
157+
columns=df.columns)
158+
tm.assert_frame_equal(result, expected)
159+
160+
def test_df_div_zero_int(self):
161+
# integer div, but deal with the 0's (GH#9144)
162+
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
163+
164+
result = df / 0
165+
expected = pd.DataFrame(np.inf, index=df.index, columns=df.columns)
166+
expected.iloc[0:3, 1] = np.nan
167+
tm.assert_frame_equal(result, expected)
168+
169+
# numpy has a slightly different (wrong) treatment
170+
with np.errstate(all='ignore'):
171+
arr = df.values.astype('float64') / 0
172+
result2 = pd.DataFrame(arr, index=df.index,
173+
columns=df.columns)
174+
tm.assert_frame_equal(result2, expected)
175+
176+
def test_df_div_zero_series_does_not_commute(self):
177+
# integer div, but deal with the 0's (GH#9144)
178+
df = pd.DataFrame(np.random.randn(10, 5))
179+
ser = df[0]
180+
res = ser / df
181+
res2 = df / ser
182+
assert not res.fillna(0).equals(res2.fillna(0))
183+
184+
65185
class TestFrameArithmetic(object):
66186

67187
@pytest.mark.xfail(reason='GH#7996 datetime64 units not converted to nano')

pandas/tests/frame/test_operators.py

-70
Original file line numberDiff line numberDiff line change
@@ -203,76 +203,6 @@ def test_timestamp_compare(self):
203203
result = right_f(Timestamp('nat'), df)
204204
assert_frame_equal(result, expected)
205205

206-
def test_modulo(self):
207-
# GH3590, modulo as ints
208-
p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
209-
210-
# this is technically wrong as the integer portion is coerced to float
211-
# ###
212-
expected = DataFrame({'first': Series([0, 0, 0, 0], dtype='float64'),
213-
'second': Series([np.nan, np.nan, np.nan, 0])})
214-
result = p % p
215-
assert_frame_equal(result, expected)
216-
217-
# numpy has a slightly different (wrong) treatement
218-
with np.errstate(all='ignore'):
219-
arr = p.values % p.values
220-
result2 = DataFrame(arr, index=p.index,
221-
columns=p.columns, dtype='float64')
222-
result2.iloc[0:3, 1] = np.nan
223-
assert_frame_equal(result2, expected)
224-
225-
result = p % 0
226-
expected = DataFrame(np.nan, index=p.index, columns=p.columns)
227-
assert_frame_equal(result, expected)
228-
229-
# numpy has a slightly different (wrong) treatement
230-
with np.errstate(all='ignore'):
231-
arr = p.values.astype('float64') % 0
232-
result2 = DataFrame(arr, index=p.index, columns=p.columns)
233-
assert_frame_equal(result2, expected)
234-
235-
# not commutative with series
236-
p = DataFrame(np.random.randn(10, 5))
237-
s = p[0]
238-
res = s % p
239-
res2 = p % s
240-
assert not res.fillna(0).equals(res2.fillna(0))
241-
242-
def test_div(self):
243-
244-
# integer div, but deal with the 0's (GH 9144)
245-
p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
246-
result = p / p
247-
248-
expected = DataFrame({'first': Series([1.0, 1.0, 1.0, 1.0]),
249-
'second': Series([nan, nan, nan, 1])})
250-
assert_frame_equal(result, expected)
251-
252-
with np.errstate(all='ignore'):
253-
arr = p.values.astype('float') / p.values
254-
result2 = DataFrame(arr, index=p.index,
255-
columns=p.columns)
256-
assert_frame_equal(result2, expected)
257-
258-
result = p / 0
259-
expected = DataFrame(np.inf, index=p.index, columns=p.columns)
260-
expected.iloc[0:3, 1] = nan
261-
assert_frame_equal(result, expected)
262-
263-
# numpy has a slightly different (wrong) treatement
264-
with np.errstate(all='ignore'):
265-
arr = p.values.astype('float64') / 0
266-
result2 = DataFrame(arr, index=p.index,
267-
columns=p.columns)
268-
assert_frame_equal(result2, expected)
269-
270-
p = DataFrame(np.random.randn(10, 5))
271-
s = p[0]
272-
res = s / p
273-
res2 = p / s
274-
assert not res.fillna(0).equals(res2.fillna(0))
275-
276206
def test_logical_operators(self):
277207

278208
def _check_bin_op(op):

pandas/tests/frame/test_timeseries.py

-9
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,3 @@ def test_tz_convert_and_localize(self, fn):
738738
with assert_raises_regex(ValueError, 'not valid'):
739739
df = DataFrame(index=l0)
740740
df = getattr(df, fn)('US/Pacific', level=1)
741-
742-
@pytest.mark.parametrize('timestamps', [
743-
[Timestamp('2012-01-01 13:00:00+00:00')] * 2,
744-
[Timestamp('2012-01-01 13:00:00')] * 2])
745-
def test_tz_aware_scalar_comparison(self, timestamps):
746-
# Test for issue #15966
747-
df = DataFrame({'test': timestamps})
748-
expected = DataFrame({'test': [False, False]})
749-
assert_frame_equal(df == -1, expected)

0 commit comments

Comments
 (0)