We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 1f2cb49 + e164b33 commit ea97682Copy full SHA for ea97682
pandas/core/common.py
@@ -1022,6 +1022,7 @@ def _possibly_downcast_to_dtype(result, dtype):
1022
if np.isscalar(result) or not len(result):
1023
return result
1024
1025
+ trans = lambda x: x
1026
if isinstance(dtype, compat.string_types):
1027
if dtype == 'infer':
1028
inferred_type = lib.infer_dtype(_ensure_object(result.ravel()))
@@ -1037,6 +1038,7 @@ def _possibly_downcast_to_dtype(result, dtype):
1037
1038
# try to upcast here
1039
elif inferred_type == 'floating':
1040
dtype = 'int64'
1041
+ trans = lambda x: x.round()
1042
1043
else:
1044
dtype = 'object'
@@ -1058,15 +1060,15 @@ def _possibly_downcast_to_dtype(result, dtype):
1058
1060
# do a test on the first element, if it fails then we are done
1059
1061
r = result.ravel()
1062
arr = np.array([ r[0] ])
- if not np.allclose(arr,arr.astype(dtype)):
1063
+ if not np.allclose(arr,trans(arr).astype(dtype)):
1064
1065
1066
# a comparable, e.g. a Decimal may slip in here
1067
elif not isinstance(r[0], (np.integer,np.floating,np.bool,int,float,bool)):
1068
1069
1070
if issubclass(result.dtype.type, (np.object_,np.number)) and notnull(result).all():
- new_result = result.astype(dtype)
1071
+ new_result = trans(result).astype(dtype)
1072
try:
1073
if np.allclose(new_result,result):
1074
return new_result
pandas/tests/test_common.py
@@ -118,6 +118,22 @@ def test_isnull_datetime():
118
assert(mask[0])
119
assert(not mask[1:].any())
120
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
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
135
136
137
138
def test_datetimeindex_from_empty_datetime64_array():
139
for unit in [ 'ms', 'us', 'ns' ]:
pandas/tests/test_generic.py
@@ -478,9 +478,9 @@ def test_nan_str_index(self):
478
def test_interp_quad(self):
479
_skip_if_no_scipy()
480
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)
+ result = sq.interpolate(method='quadratic')
+ expected = Series([1, 4, 9, 16], index=[1, 2, 3, 4])
+ assert_series_equal(result, expected)
484
485
def test_interp_scipy_basic(self):
486
@@ -659,7 +659,7 @@ def test_interp_alt_scipy(self):
659
expected = df.copy()
660
expected['A'].iloc[2] = 3
661
expected['A'].iloc[5] = 6
662
- assert_frame_equal(result, expected)
+ assert_frame_equal(result, expected.astype(np.int64))
663
664
result = df.interpolate(method='krogh')
665
expectedk = df.copy()
0 commit comments