Skip to content

Commit 6f93898

Browse files
jbrockmendeljreback
authored andcommitted
BUG: DataFrame.floordiv(ser, axis=0) not matching column-wise bheavior (#31271)
1 parent f1d7ac6 commit 6f93898

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Timezones
119119

120120
Numeric
121121
^^^^^^^
122+
- Bug in :meth:`DataFrame.floordiv` with ``axis=0`` not treating division-by-zero like :meth:`Series.floordiv` (:issue:`31271`)
122123
-
123124
-
124125

pandas/core/frame.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -5341,7 +5341,7 @@ def reorder_levels(self, order, axis=0) -> "DataFrame":
53415341
# ----------------------------------------------------------------------
53425342
# Arithmetic / combination related
53435343

5344-
def _combine_frame(self, other, func, fill_value=None, level=None):
5344+
def _combine_frame(self, other: "DataFrame", func, fill_value=None):
53455345
# at this point we have `self._indexed_same(other)`
53465346

53475347
if fill_value is None:
@@ -5368,16 +5368,18 @@ def _arith_op(left, right):
53685368

53695369
return new_data
53705370

5371-
def _combine_match_index(self, other, func):
5371+
def _combine_match_index(self, other: Series, func):
53725372
# at this point we have `self.index.equals(other.index)`
53735373

53745374
if ops.should_series_dispatch(self, other, func):
53755375
# operate column-wise; avoid costly object-casting in `.values`
53765376
new_data = ops.dispatch_to_series(self, other, func)
53775377
else:
53785378
# fastpath --> operate directly on values
5379+
other_vals = other.values.reshape(-1, 1)
53795380
with np.errstate(all="ignore"):
5380-
new_data = func(self.values.T, other.values).T
5381+
new_data = func(self.values, other_vals)
5382+
new_data = dispatch_fill_zeros(func, self.values, other_vals, new_data)
53815383
return new_data
53825384

53835385
def _construct_result(self, result) -> "DataFrame":

pandas/core/ops/missing.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,23 @@ def mask_zero_div_zero(x, y, result):
109109
return result
110110

111111
if zmask.any():
112-
shape = result.shape
113112

114113
# Flip sign if necessary for -0.0
115114
zneg_mask = zmask & np.signbit(y)
116115
zpos_mask = zmask & ~zneg_mask
117116

118-
nan_mask = (zmask & (x == 0)).ravel()
117+
nan_mask = zmask & (x == 0)
119118
with np.errstate(invalid="ignore"):
120-
neginf_mask = ((zpos_mask & (x < 0)) | (zneg_mask & (x > 0))).ravel()
121-
posinf_mask = ((zpos_mask & (x > 0)) | (zneg_mask & (x < 0))).ravel()
119+
neginf_mask = (zpos_mask & (x < 0)) | (zneg_mask & (x > 0))
120+
posinf_mask = (zpos_mask & (x > 0)) | (zneg_mask & (x < 0))
122121

123122
if nan_mask.any() or neginf_mask.any() or posinf_mask.any():
124123
# Fill negative/0 with -inf, positive/0 with +inf, 0/0 with NaN
125-
result = result.astype("float64", copy=False).ravel()
126-
127-
np.putmask(result, nan_mask, np.nan)
128-
np.putmask(result, posinf_mask, np.inf)
129-
np.putmask(result, neginf_mask, -np.inf)
124+
result = result.astype("float64", copy=False)
130125

131-
result = result.reshape(shape)
126+
result[nan_mask] = np.nan
127+
result[posinf_mask] = np.inf
128+
result[neginf_mask] = -np.inf
132129

133130
return result
134131

pandas/tests/frame/test_arithmetic.py

+15
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
332332

333333

334334
class TestFrameFlexArithmetic:
335+
def test_floordiv_axis0(self):
336+
# make sure we df.floordiv(ser, axis=0) matches column-wise result
337+
arr = np.arange(3)
338+
ser = pd.Series(arr)
339+
df = pd.DataFrame({"A": ser, "B": ser})
340+
341+
result = df.floordiv(ser, axis=0)
342+
343+
expected = pd.DataFrame({col: df[col] // ser for col in df.columns})
344+
345+
tm.assert_frame_equal(result, expected)
346+
347+
result2 = df.floordiv(ser.values, axis=0)
348+
tm.assert_frame_equal(result2, expected)
349+
335350
def test_df_add_td64_columnwise(self):
336351
# GH 22534 Check that column-wise addition broadcasts correctly
337352
dti = pd.date_range("2016-01-01", periods=10)

0 commit comments

Comments
 (0)