From c61020e601e01c544c6dc688224367bc2c4b1750 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 18 Nov 2015 22:25:12 -0500 Subject: [PATCH] BUG #11638 return correct dtype for int and float Added test case TestInferDtype --- doc/source/whatsnew/v0.17.1.txt | 1 + pandas/core/common.py | 14 +++++++++----- pandas/tests/test_common.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index c4e8ae44011ec..695c1078f06e1 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -164,6 +164,7 @@ Bug Fixes - Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`) - Bug in ``describe()`` dropping column names for hierarchical indexes (:issue:`11517`) - Bug in ``DataFrame.pct_change()`` not propagating ``axis`` keyword on ``.fillna`` method (:issue:`11150`) +- Bug in ``core.common._infer_dtype_from_scalar()`` do not upcast int and float (:issue:`11638`) diff --git a/pandas/core/common.py b/pandas/core/common.py index 4490aaf58a002..d8e21e8a7bc0c 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1002,8 +1002,7 @@ def _infer_fill_value(val): def _infer_dtype_from_scalar(val): - """ interpret the dtype from a scalar, upcast floats and ints - return the new value and the dtype """ + """ interpret the dtype from a scalar """ dtype = np.object_ @@ -1037,12 +1036,17 @@ def _infer_dtype_from_scalar(val): elif is_bool(val): dtype = np.bool_ - # provide implicity upcast on scalars elif is_integer(val): - dtype = np.int64 + if isinstance(val, int): + dtype = np.int64 + else: + dtype = type(val) elif is_float(val): - dtype = np.float64 + if isinstance(val, float): + dtype = np.float64 + else: + dtype = type(val) elif is_complex(val): dtype = np.complex_ diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 89826209fa46d..f49854cdee2f9 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -98,6 +98,37 @@ def test_abc_types(self): self.assertIsInstance(pd.Period('2012', freq='A-DEC'), com.ABCPeriod) +class TestInferDtype(tm.TestCase): + df = pd.DataFrame({'one': np.arange(6, dtype=np.int8)}) + + def test_preserves_correct_dtype(self): + # Test that data type is preserved . #5782 + + self.df.loc[1, 'one'] = 6 + self.assertEqual(self.df.dtypes.one, np.dtype(np.int8)) + self.df.one = np.int8(7) + self.assertEqual(self.df.dtypes.one, np.dtype(np.int8)) + + def test_infer_dtype_from_scalar(self): + # Test that _infer_dtype_from_scalar is returning correct dtype for int and float. + + data = np.int8(12) + dtype, val = com._infer_dtype_from_scalar(data) + self.assertEqual(dtype, np.int8) + + data = 12 + dtype, val = com._infer_dtype_from_scalar(data) + self.assertEqual(dtype, np.int64) + + data = np.float16(2.0) + dtype, val = com._infer_dtype_from_scalar(data) + self.assertEqual(dtype, np.float16) + + data = np.float(12) + dtype, val = com._infer_dtype_from_scalar(data) + self.assertEqual(dtype, np.float64) + + def test_notnull(): assert notnull(1.) assert not notnull(None)