diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index a4da249894084..74fb0e2bd54fb 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -84,7 +84,7 @@ from pandas.core.array_algos.take import take_nd from pandas.core.construction import ( - array, + array as pd_array, ensure_wrapped_if_datetimelike, extract_array, ) @@ -474,7 +474,7 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray: elif needs_i8_conversion(comps.dtype): # Dispatch to DatetimeLikeArrayMixin.isin - return array(comps).isin(values) + return pd_array(comps).isin(values) elif needs_i8_conversion(values.dtype) and not is_object_dtype(comps.dtype): # e.g. comps are integers and values are datetime64s return np.zeros(comps.shape, dtype=bool) @@ -1566,7 +1566,7 @@ def searchsorted(arr, value, side="left", sorter=None) -> np.ndarray: if is_scalar(value): value = dtype.type(value) else: - value = array(value, dtype=dtype) + value = pd_array(value, dtype=dtype) elif not ( is_object_dtype(arr) or is_numeric_dtype(arr) or is_categorical_dtype(arr) ): diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 9db21800d2499..7777cb4bf674e 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -97,7 +97,7 @@ ) import pandas.core.common as com from pandas.core.construction import ( - array, + array as pd_array, extract_array, sanitize_array, ) @@ -498,7 +498,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: # TODO: consolidate with ndarray case? elif is_extension_array_dtype(dtype): - result = array(self, dtype=dtype, copy=copy) + result = pd_array(self, dtype=dtype, copy=copy) elif is_integer_dtype(dtype) and self.isna().any(): raise ValueError("Cannot convert float NaN to integer") diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index e476c3566c10f..00a32e4443de5 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -106,7 +106,7 @@ ) import pandas.core.common as com from pandas.core.construction import ( - array, + array as pd_array, extract_array, ) from pandas.core.indexers import ( @@ -719,7 +719,7 @@ def _validate_listlike(self, value, allow_object: bool = False): # Do type inference if necessary up front # e.g. we passed PeriodIndex.values and got an ndarray of Periods - value = array(value) + value = pd_array(value) value = extract_array(value, extract_numpy=True) if is_dtype_equal(value.dtype, "string"): @@ -1207,7 +1207,7 @@ def _addsub_object_array(self, other: np.ndarray, op): assert self.shape == other.shape, (self.shape, other.shape) res_values = op(self.astype("O"), np.asarray(other)) - result = array(res_values.ravel()) + result = pd_array(res_values.ravel()) result = extract_array(result, extract_numpy=True).reshape(self.shape) return result diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 4d165dac40397..43c3a5e8bfd4c 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -76,7 +76,7 @@ from pandas.core.arrays.categorical import Categorical import pandas.core.common as com from pandas.core.construction import ( - array, + array as pd_array, ensure_wrapped_if_datetimelike, extract_array, ) @@ -661,7 +661,7 @@ def _cmp_method(self, other, op): if is_list_like(other): if len(self) != len(other): raise ValueError("Lengths must match to compare") - other = array(other) + other = pd_array(other) elif not isinstance(other, Interval): # non-interval scalar -> no matches return invalid_comparison(self, other, op) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index d0fe5b5ab0c19..db9239d03dd13 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -303,6 +303,7 @@ def array( raise ValueError(msg) if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ABCExtensionArray)): + # Note: we exclude np.ndarray here, will do type inference on it dtype = data.dtype data = extract_array(data, extract_numpy=True) diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index 42ac786ff315e..1545b5b106803 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -25,7 +25,7 @@ from pandas.core.arrays import ExtensionArray from pandas.core.arrays.sparse import SparseArray from pandas.core.construction import ( - array, + array as pd_array, ensure_wrapped_if_datetimelike, ) @@ -66,7 +66,7 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike: if is_extension_array_dtype(dtype) and isinstance(arr, np.ndarray): # numpy's astype cannot handle ExtensionDtypes - return array(arr, dtype=dtype, copy=False) + return pd_array(arr, dtype=dtype, copy=False) return arr.astype(dtype, copy=False) diff --git a/pandas/core/series.py b/pandas/core/series.py index ddfeea381ff2e..b2e620c9b8047 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -804,7 +804,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray: array(['1999-12-31T23:00:00.000000000', ...], dtype='datetime64[ns]') """ - return np.asarray(self.array, dtype) + return np.asarray(self._values, dtype) # ---------------------------------------------------------------------- # Unary Methods @@ -1798,7 +1798,7 @@ def count(self, level=None): 2 """ if level is None: - return notna(self.array).sum() + return notna(self._values).sum() elif not isinstance(self.index, MultiIndex): raise ValueError("Series.count level is only valid with a MultiIndex") @@ -2498,7 +2498,7 @@ def diff(self, periods: int = 1) -> Series: -------- {examples} """ - result = algorithms.diff(self.array, periods) + result = algorithms.diff(self._values, periods) return self._constructor(result, index=self.index).__finalize__( self, method="diff" ) @@ -3808,7 +3808,7 @@ def explode(self, ignore_index: bool = False) -> Series: if not len(self) or not is_object_dtype(self): return self.copy() - values, counts = reshape.explode(np.asarray(self.array)) + values, counts = reshape.explode(np.asarray(self._values)) if ignore_index: index = ibase.default_index(len(values)) @@ -5013,7 +5013,7 @@ def _cmp_method(self, other, op): if isinstance(other, Series) and not self._indexed_same(other): raise ValueError("Can only compare identically-labeled Series objects") - lvalues = extract_array(self, extract_numpy=True) + lvalues = self._values rvalues = extract_array(other, extract_numpy=True) res_values = ops.comparison_op(lvalues, rvalues, op) @@ -5024,7 +5024,7 @@ def _logical_method(self, other, op): res_name = ops.get_op_result_name(self, other) self, other = ops.align_method_SERIES(self, other, align_asobject=True) - lvalues = extract_array(self, extract_numpy=True) + lvalues = self._values rvalues = extract_array(other, extract_numpy=True) res_values = ops.logical_op(lvalues, rvalues, op) @@ -5034,7 +5034,7 @@ def _arith_method(self, other, op): res_name = ops.get_op_result_name(self, other) self, other = ops.align_method_SERIES(self, other) - lvalues = extract_array(self, extract_numpy=True) + lvalues = self._values rvalues = extract_array(other, extract_numpy=True) result = ops.arithmetic_op(lvalues, rvalues, op)