Skip to content

Commit 737b329

Browse files
authored
~Finish collecting m8[ns] tests, start collecting division by zero tests (#22153)
* implement box fixture, move a couple tests from timedelta.test_arithmetic, parametrize more * port floordiv tests * port test_td64arr_rfloordiv_tdlike_scalar * make fixtures, port last of TimedeltaIndex div/mul tests * port add/sub tests, cleanup imports * port test_tdi_add_offset-index * port offset addsub tests * start collecting division by zero tests * dummy commit to force CI * fix dumb name override mistake
1 parent 0041681 commit 737b329

File tree

5 files changed

+767
-636
lines changed

5 files changed

+767
-636
lines changed

pandas/tests/frame/test_arithmetic.py

-110
Original file line numberDiff line numberDiff line change
@@ -89,116 +89,6 @@ def test_df_add_flex_filled_mixed_dtypes(self):
8989
tm.assert_frame_equal(result, expected)
9090

9191

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

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

pandas/tests/indexes/test_numeric.py

-42
Original file line numberDiff line numberDiff line change
@@ -163,48 +163,6 @@ def test_divmod_series(self):
163163
for r, e in zip(result, expected):
164164
tm.assert_series_equal(r, e)
165165

166-
def test_div_zero(self, zero):
167-
idx = self.create_index()
168-
169-
expected = Index([np.nan, np.inf, np.inf, np.inf, np.inf],
170-
dtype=np.float64)
171-
result = idx / zero
172-
tm.assert_index_equal(result, expected)
173-
ser_compat = Series(idx).astype('i8') / np.array(zero).astype('i8')
174-
tm.assert_series_equal(ser_compat, Series(result))
175-
176-
def test_floordiv_zero(self, zero):
177-
idx = self.create_index()
178-
expected = Index([np.nan, np.inf, np.inf, np.inf, np.inf],
179-
dtype=np.float64)
180-
181-
result = idx // zero
182-
tm.assert_index_equal(result, expected)
183-
ser_compat = Series(idx).astype('i8') // np.array(zero).astype('i8')
184-
tm.assert_series_equal(ser_compat, Series(result))
185-
186-
def test_mod_zero(self, zero):
187-
idx = self.create_index()
188-
189-
expected = Index([np.nan, np.nan, np.nan, np.nan, np.nan],
190-
dtype=np.float64)
191-
result = idx % zero
192-
tm.assert_index_equal(result, expected)
193-
ser_compat = Series(idx).astype('i8') % np.array(zero).astype('i8')
194-
tm.assert_series_equal(ser_compat, Series(result))
195-
196-
def test_divmod_zero(self, zero):
197-
idx = self.create_index()
198-
199-
exleft = Index([np.nan, np.inf, np.inf, np.inf, np.inf],
200-
dtype=np.float64)
201-
exright = Index([np.nan, np.nan, np.nan, np.nan, np.nan],
202-
dtype=np.float64)
203-
204-
result = divmod(idx, zero)
205-
tm.assert_index_equal(result[0], exleft)
206-
tm.assert_index_equal(result[1], exright)
207-
208166
def test_explicit_conversions(self):
209167

210168
# GH 8608

0 commit comments

Comments
 (0)