forked from Bears-R-Us/arkouda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes Bears-R-Us#3782: flip function to match numpy
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |