Skip to content

BUG #11638 return correct dtype for int and float #11644

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

Closed
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)



Expand Down
14 changes: 9 additions & 5 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down Expand Up @@ -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_
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down