Skip to content

PERF: extract_array -> _values #40150

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 2 commits into from
Mar 1, 2021
Merged
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
6 changes: 3 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
)
import pandas.core.common as com
from pandas.core.construction import (
array,
array as pd_array,
extract_array,
sanitize_array,
)
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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)


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

Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down