Skip to content

Commit

Permalink
Closes Bears-R-Us#3782: flip function to match numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpotts committed Sep 24, 2024
1 parent 8dae0c5 commit af66cf1
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions arkouda/numpy/_manipulation_functions.py
Original file line number Diff line number Diff line change
@@ -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}")

0 comments on commit af66cf1

Please sign in to comment.