Skip to content

REF: Use numpy methods instead of np.array #59672

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 1 commit into from
Sep 2, 2024
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
2 changes: 1 addition & 1 deletion pandas/core/array_algos/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def quantile_with_mask(
if is_empty:
# create the array of na_values
# 2d len(values) * len(qs)
flat = np.array([fill_value] * len(qs))
flat = np.full(len(qs), fill_value)
result = np.repeat(flat, len(values)).reshape(len(values), len(qs))
else:
result = _nanquantile(
Expand Down
14 changes: 3 additions & 11 deletions pandas/core/arrays/_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
iNaT,
)

from pandas.core.construction import range_to_ndarray

if TYPE_CHECKING:
from pandas._typing import npt

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

with np.errstate(over="raise"):
# If the range is sufficiently large, np.arange may overflow
# and incorrectly return an empty array if not caught.
try:
values = np.arange(b, e, stride, dtype=np.int64)
except FloatingPointError:
Copy link
Member

Choose a reason for hiding this comment

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

Do we have coverage here?

Copy link
Member Author

@mroeschke mroeschke Aug 31, 2024

Choose a reason for hiding this comment

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

I'm assuming there's coverage in the tests added in #33498 where this prior code was added

xdr = [b]
while xdr[-1] != e:
xdr.append(xdr[-1] + stride)
values = np.array(xdr[:-1], dtype=np.int64)
return values
return range_to_ndarray(range(b, e, stride))


def _generate_range_overflow_safe(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def _hash_categories(self) -> int:
[cat_array, np.arange(len(cat_array), dtype=cat_array.dtype)]
)
else:
cat_array = np.array([cat_array])
cat_array = cat_array.reshape(1, len(cat_array))
combined_hashed = combine_hash_arrays(iter(cat_array), num_items=len(cat_array))
return np.bitwise_xor.reduce(combined_hashed)

Expand Down
7 changes: 4 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4370,11 +4370,12 @@ def post_processor(

return vals

qs = np.array(q, dtype=np.float64)
pass_qs: np.ndarray | None = qs
if is_scalar(q):
qs = np.array([q], dtype=np.float64)
pass_qs = None
pass_qs: None | np.ndarray = None
else:
qs = np.asarray(q, dtype=np.float64)
pass_qs = qs

ids = self._grouper.ids
ngroups = self._grouper.ngroups
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,9 +2686,9 @@ def _get_codes_for_sorting(self) -> list[Categorical]:
a valid valid
"""

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def _convert_listlike_datetimes(
arg, _ = maybe_convert_dtype(arg, copy=False, tz=libtimezones.maybe_get_tz(tz))
except TypeError:
if errors == "coerce":
npvalues = np.array(["NaT"], dtype="datetime64[ns]").repeat(len(arg))
npvalues = np.full(len(arg), np.datetime64("NaT", "ns"))
return DatetimeIndex(npvalues, name=name)
raise

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ def homogeneous_func(values: np.ndarray):
return values.copy()

def calc(x):
additional_nans = np.array([np.nan] * offset)
additional_nans = np.full(offset, np.nan)
x = np.concatenate((x, additional_nans))
return func(
x,
Expand Down
Loading