Skip to content

Commit ec34165

Browse files
committed
Merge pull request #5397 from jreback/infer
BUG/TST: fix downcasting of float-like object array
2 parents f5b3f8a + 4431101 commit ec34165

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,8 @@ def _possibly_downcast_to_dtype(result, dtype):
10381038
# try to upcast here
10391039
elif inferred_type == 'floating':
10401040
dtype = 'int64'
1041-
trans = lambda x: x.round()
1041+
if issubclass(result.dtype.type, np.number):
1042+
trans = lambda x: x.round()
10421043

10431044
else:
10441045
dtype = 'object'

pandas/tests/test_common.py

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ def test_downcast_conv():
135135
expected = np.array([8, 8, 8, 8, 9])
136136
assert (np.array_equal(result, expected))
137137

138+
# conversions
139+
140+
expected = np.array([1,2])
141+
for dtype in [np.float64,object,np.int64]:
142+
arr = np.array([1.0,2.0],dtype=dtype)
143+
result = com._possibly_downcast_to_dtype(arr,'infer')
144+
tm.assert_almost_equal(result, expected)
145+
146+
expected = np.array([1.0,2.0,np.nan])
147+
for dtype in [np.float64,object]:
148+
arr = np.array([1.0,2.0,np.nan],dtype=dtype)
149+
result = com._possibly_downcast_to_dtype(arr,'infer')
150+
tm.assert_almost_equal(result, expected)
151+
138152
def test_datetimeindex_from_empty_datetime64_array():
139153
for unit in [ 'ms', 'us', 'ns' ]:
140154
idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit))

0 commit comments

Comments
 (0)