From af66cf15c68a617febfed6da3d9f81398236eb1d Mon Sep 17 00:00:00 2001 From: Amanda Potts Date: Thu, 19 Sep 2024 16:00:18 -0400 Subject: [PATCH] Closes #3782: flip function to match numpy --- arkouda/numpy/_manipulation_functions.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 arkouda/numpy/_manipulation_functions.py diff --git a/arkouda/numpy/_manipulation_functions.py b/arkouda/numpy/_manipulation_functions.py new file mode 100644 index 0000000000..b431761cad --- /dev/null +++ b/arkouda/numpy/_manipulation_functions.py @@ -0,0 +1,55 @@ +from __future__ import annotations + + +from typing import List, Optional, Tuple, Union, cast +from arkouda.client import generic_msg +from arkouda.pdarrayclass import create_pdarray, create_pdarrays +from arkouda.pdarraycreation import scalar_array, promote_to_common_dtype +from arkouda.util import broadcast_dims +from arkouda.pdarrayclass import pdarray + +import numpy as np + + +def flip(x: pdarray, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None) -> pdarray: + """ + Reverse an array's values along a particular axis or axes. + + Parameters + ---------- + x : pdarray, Strings, or Categorical + Reverse the order of elements in an array along the given axis. + + The shape of the array is preserved, but the elements are reordered. + axis : int or Tuple[int, ...], optional + The axis or axes along which to flip the array. If None, flip the array along all axes. + Returns + ------- + pdarray, Strings, or Categorical + An array with the entries of axis reversed. + Note + ---- + This differs from numpy as it actually reverses the data, rather than presenting a view. + """ + axisList = [] + if axis is not None: + axisList = list(axis) if isinstance(axis, tuple) else [axis] + try: + return create_pdarray( + cast( + str, + generic_msg( + cmd=( + f"flipAll<{x.dtype},{x.ndim}>" if axis is None else f"flip<{x.dtype},{x.ndim}>" + ), + args={ + "name": x._array, + "nAxes": len(axisList), + "axis": axisList, + }, + ), + ) + ) + + except RuntimeError as e: + raise IndexError(f"Failed to flip array: {e}")