Skip to content

Commit ea97682

Browse files
committed
Merge pull request #5368 from jreback/dtype_conv
BUG: downcasting is now more robust (related GH5174)
2 parents 1f2cb49 + e164b33 commit ea97682

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

pandas/core/common.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ def _possibly_downcast_to_dtype(result, dtype):
10221022
if np.isscalar(result) or not len(result):
10231023
return result
10241024

1025+
trans = lambda x: x
10251026
if isinstance(dtype, compat.string_types):
10261027
if dtype == 'infer':
10271028
inferred_type = lib.infer_dtype(_ensure_object(result.ravel()))
@@ -1037,6 +1038,7 @@ def _possibly_downcast_to_dtype(result, dtype):
10371038
# try to upcast here
10381039
elif inferred_type == 'floating':
10391040
dtype = 'int64'
1041+
trans = lambda x: x.round()
10401042

10411043
else:
10421044
dtype = 'object'
@@ -1058,15 +1060,15 @@ def _possibly_downcast_to_dtype(result, dtype):
10581060
# do a test on the first element, if it fails then we are done
10591061
r = result.ravel()
10601062
arr = np.array([ r[0] ])
1061-
if not np.allclose(arr,arr.astype(dtype)):
1063+
if not np.allclose(arr,trans(arr).astype(dtype)):
10621064
return result
10631065

10641066
# a comparable, e.g. a Decimal may slip in here
10651067
elif not isinstance(r[0], (np.integer,np.floating,np.bool,int,float,bool)):
10661068
return result
10671069

10681070
if issubclass(result.dtype.type, (np.object_,np.number)) and notnull(result).all():
1069-
new_result = result.astype(dtype)
1071+
new_result = trans(result).astype(dtype)
10701072
try:
10711073
if np.allclose(new_result,result):
10721074
return new_result

pandas/tests/test_common.py

+16
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ def test_isnull_datetime():
118118
assert(mask[0])
119119
assert(not mask[1:].any())
120120

121+
def test_downcast_conv():
122+
# test downcasting
123+
124+
arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995])
125+
result = com._possibly_downcast_to_dtype(arr, 'infer')
126+
assert (np.array_equal(result, arr))
127+
128+
arr = np.array([8., 8., 8., 8., 8.9999999999995])
129+
result = com._possibly_downcast_to_dtype(arr, 'infer')
130+
expected = np.array([8, 8, 8, 8, 9])
131+
assert (np.array_equal(result, expected))
132+
133+
arr = np.array([8., 8., 8., 8., 9.0000000000005])
134+
result = com._possibly_downcast_to_dtype(arr, 'infer')
135+
expected = np.array([8, 8, 8, 8, 9])
136+
assert (np.array_equal(result, expected))
121137

122138
def test_datetimeindex_from_empty_datetime64_array():
123139
for unit in [ 'ms', 'us', 'ns' ]:

pandas/tests/test_generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ def test_nan_str_index(self):
478478
def test_interp_quad(self):
479479
_skip_if_no_scipy()
480480
sq = Series([1, 4, np.nan, 16], index=[1, 2, 3, 4])
481-
result = sq.interpolate(method='quadratic', downcast=False)
482-
expected = Series([1., 4, 9, 16], index=[1, 2, 3, 4])
483-
assert_series_equal(result, expected, check_less_precise=True)
481+
result = sq.interpolate(method='quadratic')
482+
expected = Series([1, 4, 9, 16], index=[1, 2, 3, 4])
483+
assert_series_equal(result, expected)
484484

485485
def test_interp_scipy_basic(self):
486486
_skip_if_no_scipy()
@@ -659,7 +659,7 @@ def test_interp_alt_scipy(self):
659659
expected = df.copy()
660660
expected['A'].iloc[2] = 3
661661
expected['A'].iloc[5] = 6
662-
assert_frame_equal(result, expected)
662+
assert_frame_equal(result, expected.astype(np.int64))
663663

664664
result = df.interpolate(method='krogh')
665665
expectedk = df.copy()

0 commit comments

Comments
 (0)