Skip to content

Commit bed38f2

Browse files
hsperrjreback
authored andcommitted
FIX: division of Decimal would crash on fill because Decimal does not support type or dtype. (GH9787)
ENH: replace np.isscalar with better lib.isscalar ADD: Test decimal division
1 parent 30580e7 commit bed38f2

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v0.16.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,5 @@ Bug Fixes
140140
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)
141141

142142
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)
143+
144+
- Bug where dividing a dataframe containing values of type ``Decimal`` by another ``Decimal`` would raise. (:issue:`9787`)

pandas/core/common.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1397,14 +1397,19 @@ def _fill_zeros(result, x, y, name, fill):
13971397
13981398
mask the nan's from x
13991399
"""
1400-
14011400
if fill is None or is_float_dtype(result):
14021401
return result
14031402

14041403
if name.startswith(('r', '__r')):
14051404
x,y = y,x
14061405

1407-
if np.isscalar(y):
1406+
is_typed_variable = (hasattr(y, 'dtype') or hasattr(y,'type'))
1407+
is_scalar = lib.isscalar(y)
1408+
1409+
if not is_typed_variable and not is_scalar:
1410+
return result
1411+
1412+
if is_scalar:
14081413
y = np.array(y)
14091414

14101415
if is_integer_dtype(y):

pandas/tests/test_series.py

+16
Original file line numberDiff line numberDiff line change
@@ -5617,6 +5617,22 @@ def test_map_type_inference(self):
56175617
s2 = s.map(lambda x: np.where(x == 0, 0, 1))
56185618
self.assertTrue(issubclass(s2.dtype.type, np.integer))
56195619

5620+
def test_divide_decimal(self):
5621+
''' resolves issue #9787 '''
5622+
from decimal import Decimal
5623+
5624+
expected = Series([Decimal(5)])
5625+
5626+
s = Series([Decimal(10)])
5627+
s = s/Decimal(2)
5628+
5629+
tm.assert_series_equal(expected, s)
5630+
5631+
s = Series([Decimal(10)])
5632+
s = s//Decimal(2)
5633+
5634+
tm.assert_series_equal(expected, s)
5635+
56205636
def test_map_decimal(self):
56215637
from decimal import Decimal
56225638

0 commit comments

Comments
 (0)