diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 82f9280870d59..4570957c01016 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -1428,13 +1428,13 @@ def diff_2d( # see https://github.com/cython/cython/issues/2646 if (out_t is float32_t and not (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)): - raise NotImplementedError + raise NotImplementedError # pragma: no cover elif (out_t is float64_t and (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)): - raise NotImplementedError + raise NotImplementedError # pragma: no cover elif out_t is int64_t and diff_t is not int64_t: # We only have out_t of int64_t if we have datetimelike - raise NotImplementedError + raise NotImplementedError # pragma: no cover else: # We put this inside an indented else block to avoid cython build # warnings about unreachable code diff --git a/pandas/_libs/arrays.pyx b/pandas/_libs/arrays.pyx index a2d4cf3000ee1..dc91e9bf755ff 100644 --- a/pandas/_libs/arrays.pyx +++ b/pandas/_libs/arrays.pyx @@ -84,7 +84,7 @@ cdef class NDArrayBacked: elif "_ndarray" in state: data = state.pop("_ndarray") else: - raise ValueError + raise ValueError # pragma: no cover self._ndarray = data self._dtype = state.pop("_dtype") @@ -95,7 +95,7 @@ cdef class NDArrayBacked: if len(state) == 1 and isinstance(state[0], dict): self.__setstate__(state[0]) return - raise NotImplementedError(state) + raise NotImplementedError(state) # pragma: no cover data, dtype = state[:2] if isinstance(dtype, np.ndarray): @@ -107,9 +107,9 @@ cdef class NDArrayBacked: for key, val in state[2].items(): setattr(self, key, val) else: - raise NotImplementedError(state) + raise NotImplementedError(state) # pragma: no cover else: - raise NotImplementedError(state) + raise NotImplementedError(state) # pragma: no cover def __len__(self) -> int: return len(self._ndarray) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index ea0bebea8299b..43fe9f1d091c8 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -264,7 +264,7 @@ cdef class IndexEngine: return algos.is_monotonic(values, timelike=False) cdef _make_hash_table(self, Py_ssize_t n): - raise NotImplementedError + raise NotImplementedError # pragma: no cover cdef _check_type(self, object val): hash(val) @@ -607,7 +607,7 @@ cdef class BaseMultiIndexCodesEngine: self._base.__init__(self, lab_ints) def _codes_to_ints(self, ndarray[uint64_t] codes) -> np.ndarray: - raise NotImplementedError("Implemented by subclass") + raise NotImplementedError("Implemented by subclass") # pragma: no cover def _extract_level_codes(self, target) -> np.ndarray: """ diff --git a/pandas/_libs/indexing.pyx b/pandas/_libs/indexing.pyx index bdbaa05138072..181de174c53fb 100644 --- a/pandas/_libs/indexing.pyx +++ b/pandas/_libs/indexing.pyx @@ -19,7 +19,7 @@ cdef class NDFrameIndexerBase: if ndim is None: ndim = self._ndim = self.obj.ndim if ndim > 2: - raise ValueError( + raise ValueError( # pragma: no cover "NDFrameIndexer does not support NDFrame objects with ndim > 2" ) return ndim diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 2f0bcefefaaa1..9fa84a0135a5e 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -338,7 +338,7 @@ cpdef Py_ssize_t slice_len(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX) except - Py_ssize_t start, stop, step, length if slc is None: - raise TypeError("slc must be slice") + raise TypeError("slc must be slice") # pragma: no cover PySlice_GetIndicesEx(slc, objlen, &start, &stop, &step, &length) @@ -358,7 +358,7 @@ cdef (Py_ssize_t, Py_ssize_t, Py_ssize_t, Py_ssize_t) slice_get_indices_ex( Py_ssize_t start, stop, step, length if slc is None: - raise TypeError("slc should be a slice") + raise TypeError("slc should be a slice") # pragma: no cover PySlice_GetIndicesEx(slc, objlen, &start, &stop, &step, &length) diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index 56be8bbfdcad2..ea5454572ca7e 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -205,7 +205,7 @@ class Resolution(Enum): elif self == Resolution.RESO_YR: return FreqGroup.FR_ANN else: - raise ValueError(self) + raise ValueError(self) # pragma: no cover @property def attrname(self) -> str: diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index ea714ce0162bc..5557882e7e9b9 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -574,7 +574,7 @@ cdef class BaseOffset: When the specific offset subclass does not have a vectorized implementation. """ - raise NotImplementedError( + raise NotImplementedError( # pragma: no cover f"DateOffset subclass {type(self).__name__} " "does not have a vectorized implementation" ) diff --git a/pandas/core/array_algos/take.py b/pandas/core/array_algos/take.py index 6d350cfa2c1d6..7de071cc6e9c3 100644 --- a/pandas/core/array_algos/take.py +++ b/pandas/core/array_algos/take.py @@ -365,6 +365,9 @@ def wrapper( ("datetime64[ns]", "datetime64[ns]"): _view_wrapper( libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64 ), + ("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper( + libalgos.take_1d_int64_int64, np.int64, np.int64, np.int64 + ), } _take_2d_axis0_dict = { @@ -394,6 +397,9 @@ def wrapper( ("datetime64[ns]", "datetime64[ns]"): _view_wrapper( libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64 ), + ("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper( + libalgos.take_2d_axis0_int64_int64, np.int64, np.int64, fill_wrap=np.int64 + ), } _take_2d_axis1_dict = { @@ -423,6 +429,9 @@ def wrapper( ("datetime64[ns]", "datetime64[ns]"): _view_wrapper( libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64 ), + ("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper( + libalgos.take_2d_axis1_int64_int64, np.int64, np.int64, fill_wrap=np.int64 + ), } _take_2d_multi_dict = { @@ -452,6 +461,9 @@ def wrapper( ("datetime64[ns]", "datetime64[ns]"): _view_wrapper( libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64 ), + ("timedelta64[ns]", "timedelta64[ns]"): _view_wrapper( + libalgos.take_2d_multi_int64_int64, np.int64, np.int64, fill_wrap=np.int64 + ), } diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b235f120d98c8..d53e965fc7d1c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2057,54 +2057,12 @@ def empty(self) -> bool_t: def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray: return np.asarray(self._values, dtype=dtype) - def __array_wrap__( - self, - result: np.ndarray, - context: tuple[Callable, tuple[Any, ...], int] | None = None, - ): - """ - Gets called after a ufunc and other functions. - - Parameters - ---------- - result: np.ndarray - The result of the ufunc or other function called on the NumPy array - returned by __array__ - context: tuple of (func, tuple, int) - This parameter is returned by ufuncs as a 3-element tuple: (name of the - ufunc, arguments of the ufunc, domain of the ufunc), but is not set by - other numpy functions.q - - Notes - ----- - Series implements __array_ufunc_ so this not called for ufunc on Series. - """ - res = lib.item_from_zerodim(result) - if is_scalar(res): - # e.g. we get here with np.ptp(series) - # ptp also requires the item_from_zerodim - return res - d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False) - # error: Argument 1 to "NDFrame" has incompatible type "ndarray"; - # expected "BlockManager" - return self._constructor(res, **d).__finalize__( # type: ignore[arg-type] - self, method="__array_wrap__" - ) - @final def __array_ufunc__( self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any ): return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs) - # ideally we would define this to avoid the getattr checks, but - # is slower - # @property - # def __array_interface__(self): - # """ provide numpy array interface method """ - # values = self.values - # return dict(typestr=values.dtype.str,shape=values.shape,data=values) - # ---------------------------------------------------------------------- # Picklability diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f6bb8e7af3558..d6408ce7bdd28 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -867,7 +867,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs): def __array_wrap__(self, result, context=None): """ - Gets called after a ufunc and other functions. + Gets called after a ufunc and other functions e.g. np.split. """ result = lib.item_from_zerodim(result) if is_bool_dtype(result) or lib.is_scalar(result) or np.ndim(result) > 1: @@ -6166,14 +6166,10 @@ def get_slice_bound( raise err if isinstance(slc, np.ndarray): - # get_loc may return a boolean array or an array of indices, which + # get_loc may return a boolean array, which # is OK as long as they are representable by a slice. - if is_bool_dtype(slc): - slc = lib.maybe_booleans_to_slice(slc.view("u1")) - else: - slc = lib.maybe_indices_to_slice( - slc.astype(np.intp, copy=False), len(self) - ) + assert is_bool_dtype(slc.dtype) + slc = lib.maybe_booleans_to_slice(slc.view("u1")) if isinstance(slc, np.ndarray): raise KeyError( f"Cannot get {side} slice bound for non-unique "