Skip to content

BUG: ensure coercing scalars to when setting as numpy #12852

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
wants to merge 1 commit into from
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
17 changes: 16 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def _infer_dtype_from_scalar(val):
dtype = np.dtype('M8[ns]')

elif isinstance(val, (np.timedelta64, timedelta)):
val = tslib.convert_to_timedelta(val, 'ns')
val = lib.Timedelta(val).value
dtype = np.dtype('m8[ns]')

elif is_bool(val):
Expand Down Expand Up @@ -826,6 +826,7 @@ def trans(x): # noqa

def _maybe_convert_string_to_object(values):
"""

Convert string-like and string-like array to convert object dtype.
This is to avoid numpy to handle the array as str dtype.
"""
Expand All @@ -837,6 +838,20 @@ def _maybe_convert_string_to_object(values):
return values


def _maybe_convert_scalar(values):
"""
Convert a scalar scalar to the appropriate dtype
This avoids numpy directly converting according to platform preferences
"""
if lib.isscalar(values):
dtype, values = _infer_dtype_from_scalar(values)
try:
values = dtype(values)
except TypeError:
pass
return values


def _lcd_dtypes(a_dtype, b_dtype):
""" return the lcd dtype to hold these types """

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
is_datetime64_dtype, is_datetimetz, is_sparse,
array_equivalent, _is_na_compat,
_maybe_convert_string_to_object,
_maybe_convert_scalar,
is_categorical, is_datetimelike_v_numeric,
is_numeric_v_string_like, is_internal_type)
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -1201,6 +1202,7 @@ def where(self, other, cond, align=True, raise_on_error=True,
"like")

other = _maybe_convert_string_to_object(other)
other = _maybe_convert_scalar(other)

# our where function
def func(cond, values, other):
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,36 @@ def test_maybe_convert_string_to_array(self):
tm.assert_numpy_array_equal(result, np.array(['x', 2], dtype=object))
self.assertTrue(result.dtype == object)

def test_maybe_convert_scalar(self):

# pass thru
result = com._maybe_convert_scalar('x')
self.assertEqual(result, 'x')
result = com._maybe_convert_scalar(np.array([1]))
self.assertEqual(result, np.array([1]))

# leave scalar dtype
result = com._maybe_convert_scalar(np.int64(1))
self.assertEqual(result, np.int64(1))
result = com._maybe_convert_scalar(np.int32(1))
self.assertEqual(result, np.int32(1))
result = com._maybe_convert_scalar(np.float32(1))
self.assertEqual(result, np.float32(1))
result = com._maybe_convert_scalar(np.int64(1))
self.assertEqual(result, np.float64(1))

# coerce
result = com._maybe_convert_scalar(1)
self.assertEqual(result, np.int64(1))
result = com._maybe_convert_scalar(1.0)
self.assertEqual(result, np.float64(1))
result = com._maybe_convert_scalar(pd.Timestamp('20130101'))
self.assertEqual(result, pd.Timestamp('20130101').value)
result = com._maybe_convert_scalar(datetime(2013, 1, 1))
self.assertEqual(result, pd.Timestamp('20130101').value)
result = com._maybe_convert_scalar(pd.Timedelta('1 day 1 min'))
self.assertEqual(result, pd.Timedelta('1 day 1 min').value)


class TestConvert(tm.TestCase):

Expand Down