Skip to content

Commit bc0bcae

Browse files
authored
PERF: extract_array -> _values (#40150)
1 parent 9da3ca2 commit bc0bcae

File tree

7 files changed

+20
-19
lines changed

7 files changed

+20
-19
lines changed

pandas/core/algorithms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484

8585
from pandas.core.array_algos.take import take_nd
8686
from pandas.core.construction import (
87-
array,
87+
array as pd_array,
8888
ensure_wrapped_if_datetimelike,
8989
extract_array,
9090
)
@@ -474,7 +474,7 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
474474

475475
elif needs_i8_conversion(comps.dtype):
476476
# Dispatch to DatetimeLikeArrayMixin.isin
477-
return array(comps).isin(values)
477+
return pd_array(comps).isin(values)
478478
elif needs_i8_conversion(values.dtype) and not is_object_dtype(comps.dtype):
479479
# e.g. comps are integers and values are datetime64s
480480
return np.zeros(comps.shape, dtype=bool)
@@ -1566,7 +1566,7 @@ def searchsorted(arr, value, side="left", sorter=None) -> np.ndarray:
15661566
if is_scalar(value):
15671567
value = dtype.type(value)
15681568
else:
1569-
value = array(value, dtype=dtype)
1569+
value = pd_array(value, dtype=dtype)
15701570
elif not (
15711571
is_object_dtype(arr) or is_numeric_dtype(arr) or is_categorical_dtype(arr)
15721572
):

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
)
9898
import pandas.core.common as com
9999
from pandas.core.construction import (
100-
array,
100+
array as pd_array,
101101
extract_array,
102102
sanitize_array,
103103
)
@@ -498,7 +498,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
498498

499499
# TODO: consolidate with ndarray case?
500500
elif is_extension_array_dtype(dtype):
501-
result = array(self, dtype=dtype, copy=copy)
501+
result = pd_array(self, dtype=dtype, copy=copy)
502502

503503
elif is_integer_dtype(dtype) and self.isna().any():
504504
raise ValueError("Cannot convert float NaN to integer")

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
)
107107
import pandas.core.common as com
108108
from pandas.core.construction import (
109-
array,
109+
array as pd_array,
110110
extract_array,
111111
)
112112
from pandas.core.indexers import (
@@ -719,7 +719,7 @@ def _validate_listlike(self, value, allow_object: bool = False):
719719

720720
# Do type inference if necessary up front
721721
# e.g. we passed PeriodIndex.values and got an ndarray of Periods
722-
value = array(value)
722+
value = pd_array(value)
723723
value = extract_array(value, extract_numpy=True)
724724

725725
if is_dtype_equal(value.dtype, "string"):
@@ -1207,7 +1207,7 @@ def _addsub_object_array(self, other: np.ndarray, op):
12071207
assert self.shape == other.shape, (self.shape, other.shape)
12081208

12091209
res_values = op(self.astype("O"), np.asarray(other))
1210-
result = array(res_values.ravel())
1210+
result = pd_array(res_values.ravel())
12111211
result = extract_array(result, extract_numpy=True).reshape(self.shape)
12121212
return result
12131213

pandas/core/arrays/interval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
from pandas.core.arrays.categorical import Categorical
7777
import pandas.core.common as com
7878
from pandas.core.construction import (
79-
array,
79+
array as pd_array,
8080
ensure_wrapped_if_datetimelike,
8181
extract_array,
8282
)
@@ -661,7 +661,7 @@ def _cmp_method(self, other, op):
661661
if is_list_like(other):
662662
if len(self) != len(other):
663663
raise ValueError("Lengths must match to compare")
664-
other = array(other)
664+
other = pd_array(other)
665665
elif not isinstance(other, Interval):
666666
# non-interval scalar -> no matches
667667
return invalid_comparison(self, other, op)

pandas/core/construction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def array(
303303
raise ValueError(msg)
304304

305305
if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ABCExtensionArray)):
306+
# Note: we exclude np.ndarray here, will do type inference on it
306307
dtype = data.dtype
307308

308309
data = extract_array(data, extract_numpy=True)

pandas/core/dtypes/concat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pandas.core.arrays import ExtensionArray
2626
from pandas.core.arrays.sparse import SparseArray
2727
from pandas.core.construction import (
28-
array,
28+
array as pd_array,
2929
ensure_wrapped_if_datetimelike,
3030
)
3131

@@ -66,7 +66,7 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
6666

6767
if is_extension_array_dtype(dtype) and isinstance(arr, np.ndarray):
6868
# numpy's astype cannot handle ExtensionDtypes
69-
return array(arr, dtype=dtype, copy=False)
69+
return pd_array(arr, dtype=dtype, copy=False)
7070
return arr.astype(dtype, copy=False)
7171

7272

pandas/core/series.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
804804
array(['1999-12-31T23:00:00.000000000', ...],
805805
dtype='datetime64[ns]')
806806
"""
807-
return np.asarray(self.array, dtype)
807+
return np.asarray(self._values, dtype)
808808

809809
# ----------------------------------------------------------------------
810810
# Unary Methods
@@ -1798,7 +1798,7 @@ def count(self, level=None):
17981798
2
17991799
"""
18001800
if level is None:
1801-
return notna(self.array).sum()
1801+
return notna(self._values).sum()
18021802
elif not isinstance(self.index, MultiIndex):
18031803
raise ValueError("Series.count level is only valid with a MultiIndex")
18041804

@@ -2498,7 +2498,7 @@ def diff(self, periods: int = 1) -> Series:
24982498
--------
24992499
{examples}
25002500
"""
2501-
result = algorithms.diff(self.array, periods)
2501+
result = algorithms.diff(self._values, periods)
25022502
return self._constructor(result, index=self.index).__finalize__(
25032503
self, method="diff"
25042504
)
@@ -3808,7 +3808,7 @@ def explode(self, ignore_index: bool = False) -> Series:
38083808
if not len(self) or not is_object_dtype(self):
38093809
return self.copy()
38103810

3811-
values, counts = reshape.explode(np.asarray(self.array))
3811+
values, counts = reshape.explode(np.asarray(self._values))
38123812

38133813
if ignore_index:
38143814
index = ibase.default_index(len(values))
@@ -5013,7 +5013,7 @@ def _cmp_method(self, other, op):
50135013
if isinstance(other, Series) and not self._indexed_same(other):
50145014
raise ValueError("Can only compare identically-labeled Series objects")
50155015

5016-
lvalues = extract_array(self, extract_numpy=True)
5016+
lvalues = self._values
50175017
rvalues = extract_array(other, extract_numpy=True)
50185018

50195019
res_values = ops.comparison_op(lvalues, rvalues, op)
@@ -5024,7 +5024,7 @@ def _logical_method(self, other, op):
50245024
res_name = ops.get_op_result_name(self, other)
50255025
self, other = ops.align_method_SERIES(self, other, align_asobject=True)
50265026

5027-
lvalues = extract_array(self, extract_numpy=True)
5027+
lvalues = self._values
50285028
rvalues = extract_array(other, extract_numpy=True)
50295029

50305030
res_values = ops.logical_op(lvalues, rvalues, op)
@@ -5034,7 +5034,7 @@ def _arith_method(self, other, op):
50345034
res_name = ops.get_op_result_name(self, other)
50355035
self, other = ops.align_method_SERIES(self, other)
50365036

5037-
lvalues = extract_array(self, extract_numpy=True)
5037+
lvalues = self._values
50385038
rvalues = extract_array(other, extract_numpy=True)
50395039
result = ops.arithmetic_op(lvalues, rvalues, op)
50405040

0 commit comments

Comments
 (0)