Skip to content

CI: fix npdev build #41665

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 5 commits 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
14 changes: 8 additions & 6 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ cdef class IndexEngine:

if self.is_monotonic_increasing:
values = self._get_index_values()
try:
left = values.searchsorted(val, side='left')
right = values.searchsorted(val, side='right')
except TypeError:
# e.g. GH#29189 get_loc(None) with a Float64Index
raise KeyError(val)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=FutureWarning)
try:
left = values.searchsorted(val, side='left')
right = values.searchsorted(val, side='right')
except TypeError:
# e.g. GH#29189 get_loc(None) with a Float64Index
raise KeyError(val)

diff = right - left
if diff == 0:
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from warnings import (
catch_warnings,
filterwarnings,
simplefilter,
warn,
)
Expand Down Expand Up @@ -1583,7 +1584,9 @@ def searchsorted(arr, value, side="left", sorter=None) -> np.ndarray:
# and `value` is a pd.Timestamp, we may need to convert value
arr = ensure_wrapped_if_datetimelike(arr)

return arr.searchsorted(value, side=side, sorter=sorter)
with catch_warnings():
filterwarnings("ignore", category=FutureWarning)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't u use create_ndarray here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc value can be a scalar here

return arr.searchsorted(value, side=side, sorter=sorter)


# ---- #
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections import abc
from datetime import (
datetime,
time,
Expand Down Expand Up @@ -71,7 +72,10 @@
from pandas.core.arrays._ranges import generate_regular_range
from pandas.core.arrays.integer import IntegerArray
import pandas.core.common as com
from pandas.core.construction import extract_array
from pandas.core.construction import (
create_ndarray,
extract_array,
)

from pandas.tseries.frequencies import get_period_alias
from pandas.tseries.offsets import (
Expand Down Expand Up @@ -2012,10 +2016,10 @@ def sequence_to_dt64ns(

if not hasattr(data, "dtype"):
# e.g. list, tuple
if np.ndim(data) == 0:
if lib.is_iterator(data) or isinstance(data, (abc.KeysView, abc.ValuesView)):
# i.e. generator
data = list(data)
data = np.asarray(data)
data = create_ndarray(data, copy=False)
copy = False
elif isinstance(data, ABCMultiIndex):
raise TypeError("Cannot create a DatetimeArray from a MultiIndex.")
Expand All @@ -2026,7 +2030,7 @@ def sequence_to_dt64ns(
data = data.to_numpy("int64", na_value=iNaT)
elif not isinstance(data, (np.ndarray, ExtensionArray)):
# GH#24539 e.g. xarray, dask object
data = np.asarray(data)
data = create_ndarray(data)

if isinstance(data, DatetimeArray):
inferred_freq = data.freq
Expand Down
16 changes: 9 additions & 7 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
)
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
from pandas.core.construction import ensure_wrapped_if_datetimelike
from pandas.core.construction import (
create_ndarray,
ensure_wrapped_if_datetimelike,
)
from pandas.core.strings.object_array import ObjectStringArrayMixin


Expand Down Expand Up @@ -94,12 +97,11 @@ def _from_sequence(
if isinstance(dtype, PandasDtype):
dtype = dtype._dtype

# error: Argument "dtype" to "asarray" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], dtype[floating[_64Bit]], Type[object],
# None]"; expected "Union[dtype[Any], None, type, _SupportsDType, str,
# Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any],
# _DTypeDict, Tuple[Any, Any]]]"
result = np.asarray(scalars, dtype=dtype) # type: ignore[arg-type]
result = create_ndarray(
scalars,
dtype=dtype, # type: ignore[arg-type]
copy=False,
)
if (
result.ndim > 1
and not hasattr(scalars, "dtype")
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections import abc
from datetime import timedelta
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -66,7 +67,10 @@
)
from pandas.core.arrays._ranges import generate_regular_range
import pandas.core.common as com
from pandas.core.construction import extract_array
from pandas.core.construction import (
create_ndarray,
extract_array,
)
from pandas.core.ops.common import unpack_zerodim_and_defer

if TYPE_CHECKING:
Expand Down Expand Up @@ -965,10 +969,10 @@ def sequence_to_td64ns(
# Unwrap whatever we have into a np.ndarray
if not hasattr(data, "dtype"):
# e.g. list, tuple
if np.ndim(data) == 0:
if lib.is_iterator(data) or isinstance(data, (abc.KeysView, abc.ValuesView)):
# i.e. generator
data = list(data)
data = np.array(data, copy=False)
data = create_ndarray(data, copy=False)
elif isinstance(data, ABCMultiIndex):
raise TypeError("Cannot create a DatetimeArray from a MultiIndex.")
else:
Expand All @@ -978,7 +982,7 @@ def sequence_to_td64ns(
data = data.to_numpy("int64", na_value=iNaT)
elif not isinstance(data, (np.ndarray, ExtensionArray)):
# GH#24539 e.g. xarray, dask object
data = np.asarray(data)
data = create_ndarray(data, copy=False)
elif isinstance(data, ABCCategorical):
data = data.categories.take(data.codes, fill_value=NaT)._values
copy = False
Expand Down
19 changes: 10 additions & 9 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def is_bool_indexer(key: Any) -> bool:
is_array_like(key) and is_extension_array_dtype(key.dtype)
):
if key.dtype == np.object_:
key = np.asarray(key)
key = np.asarray(key, dtype=object)

if not lib.is_bool_array(key):
na_msg = "Cannot mask with non-boolean array containing NA / NaN values"
Expand All @@ -142,8 +142,10 @@ def is_bool_indexer(key: Any) -> bool:
elif is_bool_dtype(key.dtype):
return True
elif isinstance(key, list):
from pandas.core.construction import create_ndarray

try:
arr = np.asarray(key)
arr = create_ndarray(key, copy=False)
return arr.dtype == np.bool_ and len(arr) == len(key)
except TypeError: # pragma: no cover
return False
Expand Down Expand Up @@ -221,6 +223,8 @@ def count_not_none(*args) -> int:


def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
if dtype is not None:
dtype = np.dtype(dtype)

if not (isinstance(values, (list, tuple)) or hasattr(values, "__array__")):
values = list(values)
Expand All @@ -229,15 +233,12 @@ def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
# expected "ndarray")
return values._values # type: ignore[return-value]

# error: Non-overlapping container check (element type: "Union[str, dtype[Any],
# None]", container item type: "type")
if isinstance(values, list) and dtype in [ # type: ignore[comparison-overlap]
np.object_,
object,
]:
if isinstance(values, list) and dtype == np.dtype("object"):
return construct_1d_object_array_from_listlike(values)

result = np.asarray(values, dtype=dtype)
from pandas.core.construction import create_ndarray

result = create_ndarray(values, dtype=dtype, copy=False)

if issubclass(result.dtype.type, str):
result = np.asarray(values, dtype=object)
Expand Down
18 changes: 18 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Sequence,
cast,
)
import warnings

import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -815,3 +816,20 @@ def create_series_with_explicit_dtype(
return Series(
data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath
)


def create_ndarray(
obj, *, dtype: np.dtype | None = None, copy: bool = True
) -> np.ndarray:
"""
Call np.ndarray if we do not know the outcome dtype.
"""
if dtype is not None:
return np.array(obj, dtype=dtype, copy=copy)
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
out = np.array(obj, copy=copy)
except (TypeError, ValueError):
out = np.array(obj, dtype=object, copy=copy)
return out
15 changes: 12 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@ def maybe_infer_dtype_type(element):
if hasattr(element, "dtype"):
tipo = element.dtype
elif is_list_like(element):
element = np.asarray(element)
from pandas.core.construction import create_ndarray

element = create_ndarray(element, copy=False)
tipo = element.dtype
return tipo

Expand Down Expand Up @@ -1608,8 +1610,9 @@ def maybe_cast_to_datetime(

if is_datetime64 or is_datetime64tz:
dtype = ensure_nanosecond_dtype(dtype)
from pandas.core.construction import create_ndarray

value = np.array(value, copy=False)
value = create_ndarray(value, copy=False)

# we have an array of datetime or timedeltas & nulls
if value.size or not is_dtype_equal(value.dtype, dtype):
Expand Down Expand Up @@ -2009,11 +2012,17 @@ def construct_1d_ndarray_preserving_na(
values, dtype, copy=copy # type: ignore[arg-type]
)
else:
from pandas.core.construction import create_ndarray

# error: Argument "dtype" to "array" has incompatible type
# "Union[dtype[Any], ExtensionDtype, None]"; expected "Union[dtype[Any],
# None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any,
# Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
subarr = np.array(values, dtype=dtype, copy=copy) # type: ignore[arg-type]
subarr = create_ndarray(
values,
dtype=dtype, # type: ignore[arg-type]
copy=copy,
)

return subarr

Expand Down
7 changes: 4 additions & 3 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from pandas.core.dtypes.generic import ABCSeries

import pandas.core.common as com
from pandas.core.construction import create_ndarray
from pandas.core.indexes.base import (
Index,
maybe_extract_name,
Expand Down Expand Up @@ -152,15 +153,15 @@ def _ensure_array(cls, data, dtype, copy: bool):
if not isinstance(data, (ABCSeries, list, tuple)):
data = list(data)

data = np.asarray(data, dtype=dtype)
data = create_ndarray(data, dtype=dtype, copy=False)

if issubclass(data.dtype.type, str):
cls._string_data_error(data)

dtype = cls._ensure_dtype(dtype)

if copy or not is_dtype_equal(data.dtype, dtype):
subarr = np.array(data, dtype=dtype, copy=copy)
subarr = create_ndarray(data, dtype=dtype, copy=copy)
cls._assert_safe_casting(data, subarr)
else:
subarr = data
Expand All @@ -169,7 +170,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
# GH#13601, GH#20285, GH#27125
raise ValueError("Index data must be 1-dimensional")

subarr = np.asarray(subarr)
subarr = create_ndarray(subarr, copy=False)
return subarr

@classmethod
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
)

import pandas.core.common as com
from pandas.core.construction import array as pd_array
from pandas.core.construction import (
array as pd_array,
create_ndarray,
)
from pandas.core.indexers import (
check_array_indexer,
is_empty_indexer,
Expand Down Expand Up @@ -1712,7 +1715,10 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
if isinstance(value, ABCDataFrame):
self._setitem_with_indexer_frame_value(indexer, value, name)

elif np.ndim(value) == 2:
elif (hasattr(value, "ndim") and value.ndim == 2) or (
not hasattr(value, "ndim")
and create_ndarray(value, copy=False).ndim == 2
):
self._setitem_with_indexer_2d_value(indexer, value)

elif len(ilocs) == 1 and lplane_indexer == len(value) and not is_scalar(pi):
Expand Down Expand Up @@ -1763,7 +1769,7 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
for loc in ilocs:
self._setitem_single_column(loc, value, pi)

def _setitem_with_indexer_2d_value(self, indexer, value):
def _setitem_with_indexer_2d_value(self, indexer, value: np.ndarray) -> None:
# We get here with np.ndim(value) == 2, excluding DataFrame,
# which goes through _setitem_with_indexer_frame_value
pi = indexer[0]
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _convert(arr):
return self.apply(_convert)

def replace(self: T, value, **kwargs) -> T:
assert np.ndim(value) == 0, value
assert not lib.is_list_like(value)
# TODO "replace" is right now implemented on the blocks, we should move
# it to general array algos so it can be reused here
return self.apply_with_block("replace", value=value, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import pandas.core.common as com
import pandas.core.computation.expressions as expressions
from pandas.core.construction import (
create_ndarray,
ensure_wrapped_if_datetimelike,
extract_array,
)
Expand Down Expand Up @@ -933,7 +934,7 @@ def setitem(self, indexer, value):
arr_value = value
else:
is_ea_value = False
arr_value = np.asarray(value)
arr_value = create_ndarray(value, copy=False)

if transpose:
values = values.T
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def convert(
)

def replace(self: T, to_replace, value, inplace: bool, regex: bool) -> T:
assert np.ndim(value) == 0, value
assert not is_list_like(value)
return self.apply(
"replace", to_replace=to_replace, value=value, inplace=inplace, regex=regex
)
Expand Down
Loading