Repository: pandas-dev/pandas

Issue:
BUG: division misbehaving
Attempting to reduce [this question](http://stackoverflow.com/questions/24024928/division-in-pandas-multiple-columns-by-another-column-of-the-same-dataframe) to a minimal test case produced:

```
>>> df = pd.DataFrame(np.arange(3*2).reshape((3,2)))
>>> df
   0  1
0  0  1
1  2  3
2  4  5
>>> df.add(df[0], axis='index')
   0  1
0  0  1
1  4  5
2  8  9
>>> df.sub(df[0], axis='index')
   0  1
0  0  1
1  0  1
2  0  1
>>> df.mul(df[0], axis='index')
    0   1
0   0   0
1   4   6
2  16  20
>>> df.div(df[0], axis='index')
Traceback (most recent call last):
  File "<ipython-input-47-5b698f939cc6>", line 1, in <module>
    df.div(df[0], axis='index')
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_5_g3b82634-py2.7-linux-x86_64.egg/pandas/core/ops.py", line 763, in f
    return self._combine_series(other, na_op, fill_value, axis, level)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_5_g3b82634-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 2830, in _combine_series
    return self._combine_match_index(other, func, level=level, fill_value=fill_value)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_5_g3b82634-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 2860, in _combine_match_index
    return self._constructor(func(left.values.T, right.values).T,
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_5_g3b82634-py2.7-linux-x86_64.egg/pandas/core/ops.py", line 754, in na_op
    result = com._fill_zeros(result, x, y, name, fill_zeros)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0_5_g3b82634-py2.7-linux-x86_64.egg/pandas/core/common.py", line 1254, in _fill_zeros
    np.putmask(result, mask & ~nans, fill)
ValueError: operands could not be broadcast together with shapes (3,) (6,) 
```

For comparison, if we change the types:

```
>>> (df*1.0).div((df*1.0)[0], axis='index')
    0         1
0 NaN       inf
1   1  1.500000
2   1  1.250000
```

Version info:

```
>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-27-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8

pandas: 0.14.0-5-g3b82634
nose: 1.3.3
Cython: 0.20.1post0
numpy: 1.8.1
scipy: 0.14.0
statsmodels: 0.5.0
IPython: 1.2.1
sphinx: None
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2014.3
bottleneck: None
tables: None
numexpr: 2.4
matplotlib: 1.3.1
openpyxl: 2.0.3
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.5.5
lxml: 3.3.3
bs4: 4.3.2
html5lib: 0.999
bq: None
apiclient: None
rpy2: None
sqlalchemy: None
pymysql: None
psycopg2: None
```



Discussion:
This however works:

```
In [3]: df=pd.DataFrame(np.reshape(np.arange(1,7),(3,2)))

In [4]: df
Out[4]: 
   0  1
0  1  2
1  3  4
2  5  6

[3 rows x 2 columns]

In [5]: print(df.div(df[0], axis='index'))
   0         1
0  1  2.000000
1  1  1.333333
2  1  1.200000

[3 rows x 2 columns]
```

What is the expected behavior when trying to divide by zero (in your `df.ix[0,0]`)?

see down a little ways here: http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#id22

`1/0 == np.inf`


Resolving patch:
diff --git a/pandas/core/common.py b/pandas/core/common.py
--- a/pandas/core/common.py
+++ b/pandas/core/common.py
@@ -1244,20 +1244,21 @@ def _fill_zeros(result, x, y, name, fill):
 
         if is_integer_dtype(y):
 
-            mask = y.ravel() == 0
-            if mask.any():
+            if (y.ravel() == 0).any():
                 shape = result.shape
                 result = result.ravel().astype('float64')
 
+                # GH 7325, mask and nans must be broadcastable
                 signs = np.sign(result)
-                nans = np.isnan(x.ravel())
-                np.putmask(result, mask & ~nans, fill)
+                mask = ((y == 0) & ~np.isnan(x)).ravel()
+
+                np.putmask(result, mask, fill)
 
                 # if we have a fill of inf, then sign it
                 # correctly
                 # GH 6178
                 if np.isinf(fill):
-                    np.putmask(result,signs<0 & mask & ~nans,-fill)
+                    np.putmask(result,signs<0 & mask, -fill)
 
                 result = result.reshape(shape)
 


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -5240,6 +5240,17 @@ def test_arith_flex_series(self):
         assert_frame_equal(df.div(row), df / row)
         assert_frame_equal(df.div(col, axis=0), (df.T / col).T)
 
+        # broadcasting issue in GH7325
+        df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='int64')
+        expected = DataFrame([[np.inf,np.inf],[1.0,1.5],[1.0,1.25]])
+        result = df.div(df[0],axis='index')
+        assert_frame_equal(result,expected)
+
+        df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='float64')
+        expected = DataFrame([[np.nan,np.inf],[1.0,1.5],[1.0,1.25]])
+        result = df.div(df[0],axis='index')
+        assert_frame_equal(result,expected)
+
     def test_arith_non_pandas_object(self):
         df = self.simple