Skip to content

Commit 17664e2

Browse files
jbrockmendeleicchen
authored andcommitted
ENH: fill_value in frame+series flex ops
1 parent 76df452 commit 17664e2

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

pandas/core/frame.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8468,7 +8468,23 @@ def _maybe_align_series_as_frame(self, series: Series, axis: AxisInt):
84688468
blockwise.
84698469
"""
84708470
rvalues = series._values
8471-
if not isinstance(rvalues, np.ndarray):
8471+
if lib.is_np_dtype(rvalues.dtype):
8472+
# We can losslessly+cheaply cast to ndarray
8473+
# i.e. ndarray or dt64[naive], td64
8474+
# TODO(EA2D): no need to special case with 2D EAs
8475+
rvalues = np.asarray(rvalues)
8476+
8477+
if axis == 0:
8478+
rvalues = rvalues.reshape(-1, 1)
8479+
else:
8480+
rvalues = rvalues.reshape(1, -1)
8481+
8482+
rvalues = np.broadcast_to(rvalues, self.shape)
8483+
# pass dtype to avoid doing inference
8484+
df = self._constructor(rvalues, dtype=rvalues.dtype)
8485+
8486+
else:
8487+
# GH#61581
84728488
if axis == 0:
84738489
df = DataFrame(dict.fromkeys(range(self.shape[1]), rvalues))
84748490
else:
@@ -8477,23 +8493,9 @@ def _maybe_align_series_as_frame(self, series: Series, axis: AxisInt):
84778493
{i: rvalues[[i]].repeat(nrows) for i in range(self.shape[1])},
84788494
dtype=rvalues.dtype,
84798495
)
8480-
df.index = self.index
8481-
df.columns = self.columns
8482-
return df
8483-
8484-
if axis == 0:
8485-
rvalues = rvalues.reshape(-1, 1)
8486-
else:
8487-
rvalues = rvalues.reshape(1, -1)
8488-
8489-
rvalues = np.broadcast_to(rvalues, self.shape)
8490-
# pass dtype to avoid doing inference
8491-
return self._constructor(
8492-
rvalues,
8493-
index=self.index,
8494-
columns=self.columns,
8495-
dtype=rvalues.dtype,
8496-
).__finalize__(series)
8496+
df.index = self.index
8497+
df.columns = self.columns
8498+
return df.__finalize__(series)
84978499

84988500
def _flex_arith_method(
84998501
self, other, op, *, axis: Axis = "columns", level=None, fill_value=None

pandas/tests/arithmetic/test_period.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,6 @@ def test_period_add_timestamp_raises(self, box_with_array):
13751375

13761376
with pytest.raises(TypeError, match=msg):
13771377
arr + pd.DataFrame([ts])
1378-
13791378
with pytest.raises(TypeError, match=msg):
13801379
pd.DataFrame([ts]) + arr
13811380

pandas/tests/frame/test_arithmetic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ def test_arith_flex_series_broadcasting(self, any_real_numpy_dtype):
659659
result = df.div(df[0], axis="index")
660660
tm.assert_frame_equal(result, expected)
661661

662+
def test_arith_flex_zero_len_raises(self):
663+
# GH 19522 passing fill_value to frame flex arith methods should
664+
# raise even in the zero-length special cases
665+
ser_len0 = Series([], dtype=object)
666+
df_len0 = DataFrame(columns=["A", "B"])
667+
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
668+
669+
msg = r"unsupported operand type\(s\) for \+: 'int' and 'str'"
670+
with pytest.raises(TypeError, match=msg):
671+
df.add(ser_len0, fill_value="E")
672+
673+
df_len0.sub(df["A"], axis=None, fill_value=3)
674+
662675
def test_flex_add_scalar_fill_value(self):
663676
# GH#12723
664677
dat = np.array([0, 1, np.nan, 3, 4, 5], dtype="float")

0 commit comments

Comments
 (0)