Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit 0fd7263

Browse files
committed
Merge pull request pandas-dev#8119 from behzadnouri/numpy-scalar
BUG: broadcasting with numpy scalars (GH8116)
2 parents b70928a + b3412b3 commit 0fd7263

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ Bug Fixes
628628
``_read`` (:issue:`7927`).
629629

630630
- Bug in ``DataFrame.stack()`` when one of the column levels was a datelike (:issue:`8039`)
631-
631+
- Bug in broadcasting numpy scalars with DataFrames (:issue:`8116`)
632632

633633

634634

pandas/core/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
784784
# casted = self._constructor_sliced(other, index=self.columns)
785785
casted = pd.Series(other, index=self.columns)
786786
return self._combine_series(casted, na_op, fill_value, axis, level)
787-
elif isinstance(other, np.ndarray):
787+
elif isinstance(other, np.ndarray) and other.ndim: # skips np scalar
788788
if other.ndim == 1:
789789
if axis is not None and self._get_axis_name(axis) == 'index':
790790
# casted = self._constructor_sliced(other,

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -4712,6 +4712,19 @@ def test_operators(self):
47124712
df = DataFrame({'a': ['a', None, 'b']})
47134713
assert_frame_equal(df + df, DataFrame({'a': ['aa', np.nan, 'bb']}))
47144714

4715+
def test_ops_np_scalar(self):
4716+
vals, xs = np.random.rand(5, 3), [nan, 7, -23, 2.718, -3.14, np.inf]
4717+
f = lambda x: DataFrame(x, index=list('ABCDE'),
4718+
columns=['jim', 'joe', 'jolie'])
4719+
4720+
df = f(vals)
4721+
4722+
for x in xs:
4723+
assert_frame_equal(df / np.array(x), f(vals / x))
4724+
assert_frame_equal(np.array(x) * df, f(vals * x))
4725+
assert_frame_equal(df + np.array(x), f(vals + x))
4726+
assert_frame_equal(np.array(x) - df, f(x - vals))
4727+
47154728
def test_operators_boolean(self):
47164729

47174730
# GH 5808

0 commit comments

Comments
 (0)