(array, shift, axis=None)
| 1911 | |
| 1912 | @derived_from(np) |
| 1913 | def roll(array, shift, axis=None): |
| 1914 | result = array |
| 1915 | |
| 1916 | if axis is None: |
| 1917 | result = ravel(result) |
| 1918 | |
| 1919 | if not isinstance(shift, Integral): |
| 1920 | raise TypeError( |
| 1921 | "Expect `shift` to be an instance of Integral when `axis` is None." |
| 1922 | ) |
| 1923 | |
| 1924 | shift = (shift,) |
| 1925 | axis = (0,) |
| 1926 | else: |
| 1927 | try: |
| 1928 | len(shift) |
| 1929 | except TypeError: |
| 1930 | shift = (shift,) |
| 1931 | try: |
| 1932 | len(axis) |
| 1933 | except TypeError: |
| 1934 | axis = (axis,) |
| 1935 | |
| 1936 | if len(shift) != len(axis): |
| 1937 | raise ValueError("Must have the same number of shifts as axes.") |
| 1938 | |
| 1939 | for i, s in zip(axis, shift): |
| 1940 | shape = result.shape[i] |
| 1941 | s = 0 if shape == 0 else -s % shape |
| 1942 | |
| 1943 | sl1 = result.ndim * [slice(None)] |
| 1944 | sl2 = result.ndim * [slice(None)] |
| 1945 | |
| 1946 | sl1[i] = slice(s, None) |
| 1947 | sl2[i] = slice(None, s) |
| 1948 | |
| 1949 | sl1 = tuple(sl1) |
| 1950 | sl2 = tuple(sl2) |
| 1951 | |
| 1952 | result = concatenate([result[sl1], result[sl2]], axis=i) |
| 1953 | |
| 1954 | result = result.reshape(array.shape) |
| 1955 | # Ensure that the output is always a new array object |
| 1956 | result = result.copy() if result is array else result |
| 1957 | |
| 1958 | return result |
| 1959 | |
| 1960 | |
| 1961 | @derived_from(np) |
nothing calls this directly
no test coverage detected