Skip to content

REF: simplify try_cast #41811

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

Merged
merged 3 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 6 additions & 7 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)
from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
construct_1d_ndarray_preserving_na,
construct_1d_object_array_from_listlike,
maybe_cast_to_datetime,
maybe_cast_to_integer_array,
Expand Down Expand Up @@ -735,6 +734,10 @@ def _try_cast(
return subarr
return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy)

elif dtype.kind == "U":
# TODO: test cases with arr.dtype.kind in ["m", "M"]
return lib.ensure_string_array(arr, convert_na_value=False, copy=copy)

elif dtype.kind in ["m", "M"]:
return maybe_cast_to_datetime(arr, dtype)

Expand All @@ -744,16 +747,12 @@ def _try_cast(
if is_integer_dtype(dtype):
# this will raise if we have e.g. floats

maybe_cast_to_integer_array(arr, dtype)
subarr = arr
subarr = maybe_cast_to_integer_array(arr, dtype)
else:
subarr = arr

if not isinstance(subarr, ABCExtensionArray):
# 4 tests fail if we move this to a try/except/else; see
# test_constructor_compound_dtypes, test_constructor_cast_failure
# test_constructor_dict_cast2, test_loc_setitem_dtype
subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy)
subarr = np.array(arr, dtype=dtype, copy=copy)

except (ValueError, TypeError):
if raise_cast_failure:
Expand Down
54 changes: 0 additions & 54 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from typing import (
TYPE_CHECKING,
Any,
Sequence,
Sized,
cast,
overload,
Expand Down Expand Up @@ -1965,59 +1964,6 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
return result


def construct_1d_ndarray_preserving_na(
values: Sequence, dtype: np.dtype | None = None, copy: bool = False
) -> np.ndarray:
"""
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.

Parameters
----------
values : Sequence
dtype : numpy.dtype, optional
copy : bool, default False
Note that copies may still be made with ``copy=False`` if casting
is required.

Returns
-------
arr : ndarray[dtype]

Examples
--------
>>> np.array([1.0, 2.0, None], dtype='str')
array(['1.0', '2.0', 'None'], dtype='<U4')

>>> construct_1d_ndarray_preserving_na([1.0, 2.0, None], dtype=np.dtype('str'))
array(['1.0', '2.0', None], dtype=object)
"""

if dtype is not None and dtype.kind == "U":
subarr = lib.ensure_string_array(values, convert_na_value=False, copy=copy)
else:
if dtype is not None:
_disallow_mismatched_datetimelike(values, dtype)

if (
dtype == object
and isinstance(values, np.ndarray)
and values.dtype.kind in ["m", "M"]
):
# TODO(numpy#12550): special-case can be removed
subarr = construct_1d_object_array_from_listlike(list(values))
elif (
dtype is not None
and dtype.kind in ["i", "u"]
and isinstance(values, np.ndarray)
and values.dtype.kind == "f"
):
return astype_float_to_int_nansafe(values, dtype, copy=copy)
else:
subarr = np.array(values, dtype=dtype, copy=copy)

return subarr


def maybe_cast_to_integer_array(
arr: list | np.ndarray, dtype: np.dtype, copy: bool = False
) -> np.ndarray:
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/dtypes/cast/test_construct_ndarray.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import numpy as np
import pytest

from pandas.core.dtypes.cast import construct_1d_ndarray_preserving_na

import pandas._testing as tm
from pandas.core.construction import sanitize_array


@pytest.mark.parametrize(
Expand All @@ -17,7 +16,7 @@
],
)
def test_construct_1d_ndarray_preserving_na(values, dtype, expected):
result = construct_1d_ndarray_preserving_na(values, dtype=dtype)
result = sanitize_array(values, index=None, dtype=dtype)
tm.assert_numpy_array_equal(result, expected)


Expand All @@ -27,5 +26,5 @@ def test_construct_1d_ndarray_preserving_na_datetimelike(dtype):
expected = np.array(list(arr), dtype=object)
assert all(isinstance(x, type(arr[0])) for x in expected)

result = construct_1d_ndarray_preserving_na(arr, np.dtype(object))
result = sanitize_array(arr, index=None, dtype=np.dtype(object))
tm.assert_numpy_array_equal(result, expected)