Skip to content

Commit

Permalink
Use asarray where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Mar 5, 2024
1 parent 86f3542 commit 2f6fd60
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 98 deletions.
10 changes: 3 additions & 7 deletions pandas/core/array_algos/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import numpy as np

from pandas.compat.numpy import np_version_gt2

from pandas.core.dtypes.missing import (
isna,
na_value_for_dtype,
Expand Down Expand Up @@ -104,8 +102,7 @@ def quantile_with_mask(
interpolation=interpolation,
)

copy_false = None if np_version_gt2 else False
result = np.array(result, copy=copy_false)
result = np.asarray(result)
result = result.T

return result
Expand Down Expand Up @@ -202,12 +199,11 @@ def _nanpercentile(
_nanpercentile_1d(val, m, qs, na_value, interpolation=interpolation)
for (val, m) in zip(list(values), list(mask))
]
copy_false = None if np_version_gt2 else False
if values.dtype.kind == "f":
# preserve itemsize
result = np.array(result, dtype=values.dtype, copy=copy_false).T
result = np.asarray(result, dtype=values.dtype).T
else:
result = np.array(result, copy=copy_false).T
result = np.asarray(result).T
if (
result.dtype != values.dtype
and not mask.all()
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
lib,
)
from pandas.compat import set_function_name
from pandas.compat.numpy import (
function as nv,
np_version_gt2,
)
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -713,8 +710,6 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
return self
else:
return self.copy()
if np_version_gt2 and not copy:
copy = None

if isinstance(dtype, ExtensionDtype):
cls = dtype.construct_array_type()
Expand All @@ -730,7 +725,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:

return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)

return np.array(self, dtype=dtype, copy=copy)
if not copy:
return np.asarray(self, dtype=dtype)
else:
return np.array(self, dtype=dtype, copy=copy)

def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
"""
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
tzconversion,
)
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
from pandas.compat.numpy import np_version_gt2
from pandas.errors import PerformanceWarning
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_inclusive
Expand Down Expand Up @@ -2422,8 +2421,7 @@ def objects_to_datetime64(
assert errors in ["raise", "coerce"]

# if str-dtype, convert
copy_false = None if np_version_gt2 else False
data = np.array(data, dtype=np.object_, copy=copy_false)
data = np.asarray(data, dtype=np.object_)

result, tz_parsed = tslib.array_to_datetime(
data,
Expand Down
21 changes: 10 additions & 11 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
lib,
missing as libmissing,
)
from pandas.compat.numpy import np_version_gt2
from pandas.errors import AbstractMethodError
from pandas.util._decorators import cache_readonly

Expand Down Expand Up @@ -138,12 +137,6 @@ def _coerce_to_data_and_mask(
values, dtype, copy: bool, dtype_cls: type[NumericDtype], default_dtype: np.dtype
):
checker = dtype_cls._checker
if np_version_gt2:
copy_false = None
if not copy:
copy = None
else:
copy_false = False

mask = None
inferred_type = None
Expand All @@ -167,7 +160,10 @@ def _coerce_to_data_and_mask(
return values, mask, dtype, inferred_type

original = values
values = np.array(values, copy=copy)
if not copy:
values = np.asarray(values)
else:
values = np.array(values, copy=copy)
inferred_type = None
if values.dtype == object or is_string_dtype(values.dtype):
inferred_type = lib.infer_dtype(values, skipna=True)
Expand All @@ -176,7 +172,10 @@ def _coerce_to_data_and_mask(
raise TypeError(f"{values.dtype} cannot be converted to {name}")

elif values.dtype.kind == "b" and checker(dtype):
values = np.array(values, dtype=default_dtype, copy=copy)
if not copy:
values = np.asarray(values, dtype=default_dtype)
else:
values = np.array(values, dtype=default_dtype, copy=copy)

elif values.dtype.kind not in "iuf":
name = dtype_cls.__name__.strip("_")
Expand Down Expand Up @@ -215,9 +214,9 @@ def _coerce_to_data_and_mask(
inferred_type not in ["floating", "mixed-integer-float"]
and not mask.any()
):
values = np.array(original, dtype=dtype, copy=copy_false)
values = np.asarray(original, dtype=dtype)
else:
values = np.array(original, dtype="object", copy=copy_false)
values = np.asarray(original, dtype="object")

# we copy as need to coerce here
if mask.any():
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
get_period_field_arr,
period_asfreq_arr,
)
from pandas.compat.numpy import np_version_gt2
from pandas.util._decorators import (
cache_readonly,
doc,
Expand Down Expand Up @@ -244,9 +243,6 @@ def __init__(
if not isinstance(dtype, PeriodDtype):
raise ValueError(f"Invalid dtype {dtype} for PeriodArray")

if np_version_gt2 and not copy:
copy = None

if isinstance(values, ABCSeries):
values = values._values
if not isinstance(values, type(self)):
Expand All @@ -260,7 +256,10 @@ def __init__(
raise raise_on_incompatible(values, dtype.freq)
values, dtype = values._ndarray, values.dtype

values = np.array(values, dtype="int64", copy=copy)
if not copy:
values = np.asarray(values, dtype="int64")
else:
values = np.array(values, dtype="int64", copy=copy)
if dtype is None:
raise ValueError("dtype is not specified and cannot be inferred")
dtype = cast(PeriodDtype, dtype)
Expand Down
14 changes: 5 additions & 9 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
parse_timedelta_unit,
truediv_object_array,
)
from pandas.compat.numpy import (
function as nv,
np_version_gt2,
)
from pandas.compat.numpy import function as nv
from pandas.util._validators import validate_endpoints

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1075,10 +1072,10 @@ def sequence_to_td64ns(
# This includes datetime64-dtype, see GH#23539, GH#29794
raise TypeError(f"dtype {data.dtype} cannot be converted to timedelta64[ns]")

copy_false = None if np_version_gt2 else False
if not copy:
copy = copy_false
data = np.array(data, copy=copy)
data = np.asarray(data)
else:
data = np.array(data, copy=copy)

assert data.dtype.kind == "m"
assert data.dtype != "m8" # i.e. not unit-less
Expand Down Expand Up @@ -1158,8 +1155,7 @@ def _objects_to_td64ns(
higher level.
"""
# coerce Index to np.ndarray, converting string-dtype if necessary
copy_false = None if np_version_gt2 else False
values = np.array(data, dtype=np.object_, copy=copy_false)
values = np.asarray(data, dtype=np.object_)

result = array_to_timedelta64(values, unit=unit, errors=errors)
return result.view("timedelta64[ns]")
Expand Down
22 changes: 11 additions & 11 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
DtypeObj,
T,
)
from pandas.compat.numpy import np_version_gt2
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.base import ExtensionDtype
Expand Down Expand Up @@ -627,9 +626,10 @@ def sanitize_array(

elif hasattr(data, "__array__"):
# e.g. dask array GH#38645
if np_version_gt2 and not copy:
copy = None
data = np.array(data, copy=copy)
if not copy:
data = np.asarray(data)
else:
data = np.array(data, copy=copy)
return sanitize_array(
data,
index=index,
Expand Down Expand Up @@ -738,9 +738,6 @@ def _sanitize_str_dtypes(
"""
Ensure we have a dtype that is supported by pandas.
"""
copy_false = None if np_version_gt2 else False
if not copy:
copy = copy_false

# This is to prevent mixed-type Series getting all casted to
# NumPy string type, e.g. NaN --> '-1#IND'.
Expand All @@ -750,8 +747,11 @@ def _sanitize_str_dtypes(
# GH#19853: If data is a scalar, result has already the result
if not lib.is_scalar(data):
if not np.all(isna(data)):
data = np.array(data, dtype=dtype, copy=copy_false)
result = np.array(data, dtype=object, copy=copy)
data = np.asarray(data, dtype=dtype)
if not copy:
result = np.asarray(data, dtype=object)
else:
result = np.array(data, dtype=object, copy=copy)
return result


Expand Down Expand Up @@ -787,8 +787,6 @@ def _try_cast(
np.ndarray or ExtensionArray
"""
is_ndarray = isinstance(arr, np.ndarray)
if np_version_gt2 and not copy:
copy = None

if dtype == object:
if not is_ndarray:
Expand Down Expand Up @@ -818,6 +816,8 @@ def _try_cast(
# this will raise if we have e.g. floats

subarr = maybe_cast_to_integer_array(arr, dtype)
elif not copy:
subarr = np.asarray(arr, dtype=dtype)
else:
subarr = np.array(arr, dtype=dtype, copy=copy)

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,9 +1491,6 @@ def construct_2d_arraylike_from_scalar(
value: Scalar, length: int, width: int, dtype: np.dtype, copy: bool
) -> np.ndarray:
shape = (length, width)
copy_false = None if np_version_gt2 else False
if not copy:
copy = copy_false

if dtype.kind in "mM":
value = _maybe_box_and_unbox_datetimelike(value, dtype)
Expand All @@ -1506,7 +1503,10 @@ def construct_2d_arraylike_from_scalar(

# Attempt to coerce to a numpy array
try:
arr = np.array(value, dtype=dtype, copy=copy)
if not copy:
arr = np.asarray(value, dtype=dtype)
else:
arr = np.array(value, dtype=dtype, copy=copy)
except (ValueError, TypeError) as err:
raise TypeError(
f"DataFrame constructor called with incompatible data and dtype: {err}"
Expand Down Expand Up @@ -1655,8 +1655,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
"out-of-bound Python int",
DeprecationWarning,
)
copy_false = None if np_version_gt2 else False
casted = np.array(arr, dtype=dtype, copy=copy_false)
casted = np.asarray(arr, dtype=dtype)
else:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
NaT,
iNaT,
)
from pandas.compat.numpy import np_version_gt2

from pandas.core.dtypes.common import (
DT64NS_DTYPE,
Expand Down Expand Up @@ -565,8 +564,7 @@ def infer_fill_value(val):
"""
if not is_list_like(val):
val = [val]
copy_false = None if np_version_gt2 else False
val = np.array(val, copy=copy_false)
val = np.asarray(val)
if val.dtype.kind in "mM":
return np.array("NaT", dtype=val.dtype)
elif val.dtype == object:
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
Shape,
npt,
)
from pandas.compat.numpy import (
function as nv,
np_version_gt2,
)
from pandas.compat.numpy import function as nv
from pandas.errors import (
InvalidIndexError,
PerformanceWarning,
Expand Down Expand Up @@ -773,8 +770,7 @@ def _values(self) -> np.ndarray:
):
vals = vals.astype(object)

copy_false = None if np_version_gt2 else False
array_vals = np.array(vals, copy=copy_false)
array_vals = np.asarray(vals)
array_vals = algos.take_nd(array_vals, codes, fill_value=index._na_value)
values.append(array_vals)

Expand Down Expand Up @@ -3361,8 +3357,7 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
locs = (level_codes >= idx.start) & (level_codes < idx.stop)
return locs

copy_false = None if np_version_gt2 else False
locs = np.array(level_codes == idx, dtype=bool, copy=copy_false)
locs = np.asarray(level_codes == idx, dtype=bool)

if not locs.any():
# The label is present in self.levels[level] but unused:
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
BlockValuesRefs,
)
from pandas._libs.tslibs import Timestamp
from pandas.compat.numpy import np_version_gt2
from pandas.errors import (
AbstractMethodError,
PerformanceWarning,
Expand Down Expand Up @@ -1798,9 +1797,6 @@ def as_array(
arr : ndarray
"""
passed_nan = lib.is_float(na_value) and isna(na_value)
copy_false = None if np_version_gt2 else False
if not copy:
copy = copy_false

if len(self.blocks) == 0:
arr = np.empty(self.shape, dtype=float)
Expand Down Expand Up @@ -1828,6 +1824,8 @@ def as_array(
na_value=na_value,
copy=copy,
).reshape(blk.shape)
elif not copy:
arr = np.asarray(blk.values, dtype=dtype)
else:
arr = np.array(blk.values, dtype=dtype, copy=copy)

Expand Down
4 changes: 1 addition & 3 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from pandas._libs.lib import is_string_array
from pandas._libs.tslibs import timezones
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import np_version_gt2
from pandas.compat.pickle_compat import patch_pickle
from pandas.errors import (
AttributeConflictWarning,
Expand Down Expand Up @@ -4044,8 +4043,7 @@ def _create_axes(
if isinstance(data_converted.dtype, CategoricalDtype):
ordered = data_converted.ordered
meta = "category"
copy_false = None if np_version_gt2 else False
metadata = np.array(data_converted.categories, copy=copy_false).ravel()
metadata = np.asarray(data_converted.categories).ravel()

data, dtype_name = _get_data_and_dtype_name(data_converted)

Expand Down

0 comments on commit 2f6fd60

Please sign in to comment.