Skip to content

Commit c61020e

Browse files
BUG #11638 return correct dtype for int and float
Added test case TestInferDtype
1 parent 12d9938 commit c61020e

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Bug Fixes
164164
- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`)
165165
- Bug in ``describe()`` dropping column names for hierarchical indexes (:issue:`11517`)
166166
- Bug in ``DataFrame.pct_change()`` not propagating ``axis`` keyword on ``.fillna`` method (:issue:`11150`)
167+
- Bug in ``core.common._infer_dtype_from_scalar()`` do not upcast int and float (:issue:`11638`)
167168

168169

169170

pandas/core/common.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,7 @@ def _infer_fill_value(val):
10021002

10031003

10041004
def _infer_dtype_from_scalar(val):
1005-
""" interpret the dtype from a scalar, upcast floats and ints
1006-
return the new value and the dtype """
1005+
""" interpret the dtype from a scalar """
10071006

10081007
dtype = np.object_
10091008

@@ -1037,12 +1036,17 @@ def _infer_dtype_from_scalar(val):
10371036
elif is_bool(val):
10381037
dtype = np.bool_
10391038

1040-
# provide implicity upcast on scalars
10411039
elif is_integer(val):
1042-
dtype = np.int64
1040+
if isinstance(val, int):
1041+
dtype = np.int64
1042+
else:
1043+
dtype = type(val)
10431044

10441045
elif is_float(val):
1045-
dtype = np.float64
1046+
if isinstance(val, float):
1047+
dtype = np.float64
1048+
else:
1049+
dtype = type(val)
10461050

10471051
elif is_complex(val):
10481052
dtype = np.complex_

pandas/tests/test_common.py

+31
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,37 @@ def test_abc_types(self):
9898
self.assertIsInstance(pd.Period('2012', freq='A-DEC'), com.ABCPeriod)
9999

100100

101+
class TestInferDtype(tm.TestCase):
102+
df = pd.DataFrame({'one': np.arange(6, dtype=np.int8)})
103+
104+
def test_preserves_correct_dtype(self):
105+
# Test that data type is preserved . #5782
106+
107+
self.df.loc[1, 'one'] = 6
108+
self.assertEqual(self.df.dtypes.one, np.dtype(np.int8))
109+
self.df.one = np.int8(7)
110+
self.assertEqual(self.df.dtypes.one, np.dtype(np.int8))
111+
112+
def test_infer_dtype_from_scalar(self):
113+
# Test that _infer_dtype_from_scalar is returning correct dtype for int and float.
114+
115+
data = np.int8(12)
116+
dtype, val = com._infer_dtype_from_scalar(data)
117+
self.assertEqual(dtype, np.int8)
118+
119+
data = 12
120+
dtype, val = com._infer_dtype_from_scalar(data)
121+
self.assertEqual(dtype, np.int64)
122+
123+
data = np.float16(2.0)
124+
dtype, val = com._infer_dtype_from_scalar(data)
125+
self.assertEqual(dtype, np.float16)
126+
127+
data = np.float(12)
128+
dtype, val = com._infer_dtype_from_scalar(data)
129+
self.assertEqual(dtype, np.float64)
130+
131+
101132
def test_notnull():
102133
assert notnull(1.)
103134
assert not notnull(None)

0 commit comments

Comments
 (0)