Skip to content

Commit e07453e

Browse files
authored
REF: Use numpy methods instead of np.array (#59672)
1 parent 078e11f commit e07453e

File tree

7 files changed

+13
-20
lines changed

7 files changed

+13
-20
lines changed

pandas/core/array_algos/quantile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def quantile_with_mask(
9191
if is_empty:
9292
# create the array of na_values
9393
# 2d len(values) * len(qs)
94-
flat = np.array([fill_value] * len(qs))
94+
flat = np.full(len(qs), fill_value)
9595
result = np.repeat(flat, len(values)).reshape(len(values), len(qs))
9696
else:
9797
result = _nanquantile(

pandas/core/arrays/_ranges.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
iNaT,
1919
)
2020

21+
from pandas.core.construction import range_to_ndarray
22+
2123
if TYPE_CHECKING:
2224
from pandas._typing import npt
2325

@@ -82,17 +84,7 @@ def generate_regular_range(
8284
"at least 'start' or 'end' should be specified if a 'period' is given."
8385
)
8486

85-
with np.errstate(over="raise"):
86-
# If the range is sufficiently large, np.arange may overflow
87-
# and incorrectly return an empty array if not caught.
88-
try:
89-
values = np.arange(b, e, stride, dtype=np.int64)
90-
except FloatingPointError:
91-
xdr = [b]
92-
while xdr[-1] != e:
93-
xdr.append(xdr[-1] + stride)
94-
values = np.array(xdr[:-1], dtype=np.int64)
95-
return values
87+
return range_to_ndarray(range(b, e, stride))
9688

9789

9890
def _generate_range_overflow_safe(

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def _hash_categories(self) -> int:
513513
[cat_array, np.arange(len(cat_array), dtype=cat_array.dtype)]
514514
)
515515
else:
516-
cat_array = np.array([cat_array])
516+
cat_array = cat_array.reshape(1, len(cat_array))
517517
combined_hashed = combine_hash_arrays(iter(cat_array), num_items=len(cat_array))
518518
return np.bitwise_xor.reduce(combined_hashed)
519519

pandas/core/groupby/groupby.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4370,11 +4370,12 @@ def post_processor(
43704370

43714371
return vals
43724372

4373-
qs = np.array(q, dtype=np.float64)
4374-
pass_qs: np.ndarray | None = qs
43754373
if is_scalar(q):
43764374
qs = np.array([q], dtype=np.float64)
4377-
pass_qs = None
4375+
pass_qs: None | np.ndarray = None
4376+
else:
4377+
qs = np.asarray(q, dtype=np.float64)
4378+
pass_qs = qs
43784379

43794380
ids = self._grouper.ids
43804381
ngroups = self._grouper.ngroups

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2686,9 +2686,9 @@ def _get_codes_for_sorting(self) -> list[Categorical]:
26862686
a valid valid
26872687
"""
26882688

2689-
def cats(level_codes):
2689+
def cats(level_codes: np.ndarray) -> np.ndarray:
26902690
return np.arange(
2691-
np.array(level_codes).max() + 1 if len(level_codes) else 0,
2691+
level_codes.max() + 1 if len(level_codes) else 0,
26922692
dtype=level_codes.dtype,
26932693
)
26942694

pandas/core/tools/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def _convert_listlike_datetimes(
418418
arg, _ = maybe_convert_dtype(arg, copy=False, tz=libtimezones.maybe_get_tz(tz))
419419
except TypeError:
420420
if errors == "coerce":
421-
npvalues = np.array(["NaT"], dtype="datetime64[ns]").repeat(len(arg))
421+
npvalues = np.full(len(arg), np.datetime64("NaT", "ns"))
422422
return DatetimeIndex(npvalues, name=name)
423423
raise
424424

pandas/core/window/rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ def homogeneous_func(values: np.ndarray):
11861186
return values.copy()
11871187

11881188
def calc(x):
1189-
additional_nans = np.array([np.nan] * offset)
1189+
additional_nans = np.full(offset, np.nan)
11901190
x = np.concatenate((x, additional_nans))
11911191
return func(
11921192
x,

0 commit comments

Comments
 (0)