Skip to content

BUG: downcasting is now more robust (related GH5174) #5368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ def _possibly_downcast_to_dtype(result, dtype):
if np.isscalar(result) or not len(result):
return result

trans = lambda x: x
if isinstance(dtype, compat.string_types):
if dtype == 'infer':
inferred_type = lib.infer_dtype(_ensure_object(result.ravel()))
Expand All @@ -1037,6 +1038,7 @@ def _possibly_downcast_to_dtype(result, dtype):
# try to upcast here
elif inferred_type == 'floating':
dtype = 'int64'
trans = lambda x: x.round()

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

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

if issubclass(result.dtype.type, (np.object_,np.number)) and notnull(result).all():
new_result = result.astype(dtype)
new_result = trans(result).astype(dtype)
try:
if np.allclose(new_result,result):
return new_result
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ def test_isnull_datetime():
assert(mask[0])
assert(not mask[1:].any())

def test_downcast_conv():
# test downcasting

arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995])
result = com._possibly_downcast_to_dtype(arr, 'infer')
assert (np.array_equal(result, arr))

arr = np.array([8., 8., 8., 8., 8.9999999999995])
result = com._possibly_downcast_to_dtype(arr, 'infer')
expected = np.array([8, 8, 8, 8, 9])
assert (np.array_equal(result, expected))

arr = np.array([8., 8., 8., 8., 9.0000000000005])
result = com._possibly_downcast_to_dtype(arr, 'infer')
expected = np.array([8, 8, 8, 8, 9])
assert (np.array_equal(result, expected))

def test_datetimeindex_from_empty_datetime64_array():
for unit in [ 'ms', 'us', 'ns' ]:
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ def test_nan_str_index(self):
def test_interp_quad(self):
_skip_if_no_scipy()
sq = Series([1, 4, np.nan, 16], index=[1, 2, 3, 4])
result = sq.interpolate(method='quadratic', downcast=False)
expected = Series([1., 4, 9, 16], index=[1, 2, 3, 4])
assert_series_equal(result, expected, check_less_precise=True)
result = sq.interpolate(method='quadratic')
expected = Series([1, 4, 9, 16], index=[1, 2, 3, 4])
assert_series_equal(result, expected)

def test_interp_scipy_basic(self):
_skip_if_no_scipy()
Expand Down Expand Up @@ -659,7 +659,7 @@ def test_interp_alt_scipy(self):
expected = df.copy()
expected['A'].iloc[2] = 3
expected['A'].iloc[5] = 6
assert_frame_equal(result, expected)
assert_frame_equal(result, expected.astype(np.int64))

result = df.interpolate(method='krogh')
expectedk = df.copy()
Expand Down