1

I am in a position to extract whole data from an array. The simplest method would be simply passing array[:]. However, I want to make it automated as part of the larger project where the index would be varying with the data format. Therefore, is it possible to extract the whole data by passing a slicing string ":" as an index to the array?

To make things clear, here is an example of what I am trying to do.

create an array:

>>> import numpy as np
>>> a = np.random.randint(0,10,(5,5))
>>> a
array([[3, 3, 3, 7, 2],
       [8, 6, 8, 6, 3],
       [4, 2, 2, 0, 3],
       [4, 0, 6, 0, 1],
       [1, 2, 0, 2, 8]])

General slicing of dataset using tradition method:

>>> a[:]
array([[3, 3, 3, 7, 2],
       [8, 6, 8, 6, 3],
       [4, 2, 2, 0, 3],
       [4, 0, 6, 0, 1],
       [1, 2, 0, 2, 8]])

It works. However, I intend to make : as a variable and try to extract with above example like below:

>>> b = ":"
>>> a[b]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

As per the printed error, I did some correction while defining variable as indicated below which also resulted in error:

>>> c = slice(':')
>>> a[c]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method

So, is it possible at all to extract data by passing a slicing string ":" as an index to an array?

Update

Thank you all for your comments. It is possible with following method:

>>> c = np.index_exp[:]
>>> a[c]
array([[3, 3, 3, 7, 2],
       [8, 6, 8, 6, 3],
       [4, 2, 2, 0, 3],
       [4, 0, 6, 0, 1],
       [1, 2, 0, 2, 8]])
sundar_ima
  • 3,604
  • 8
  • 33
  • 52
  • No, it's not possible because you break the syntax and I honestly have no idea why you would need to do this – roganjosh Feb 19 '23 at 15:41
  • It makes no sense at all that you'd need this string literal to make an array slice that's dynamic. You've gone off piste – roganjosh Feb 19 '23 at 15:42
  • 2
    Why do you need it to be a string? Can't you use a `slice` object? You could do `b = slice(None,None)` instead of `b = ":"`. – Donald Duck Feb 19 '23 at 15:44
  • I have used `index` to extract data extensively in the project changing it to slice would require lot of work. Anyway, it is possible with `index = np.index_exp[:]` as I did go through some other forum posts. Thank you. Will update the post with the answer. – sundar_ima Feb 19 '23 at 15:47
  • Does this answer your question? [python create slice object from string](https://stackoverflow.com/questions/680826/python-create-slice-object-from-string) – Donald Duck Feb 19 '23 at 15:51
  • No. This one https://stackoverflow.com/questions/12616821/numpy-slicing-from-variable – sundar_ima Feb 19 '23 at 15:57
  • Using `np.index_exp[:]` is the same as using `slice(None)`. It's just letting the interpreter make the translation. You might find the whole `numpy.lib.index_tricks.py` file to instructive reading. – hpaulj Feb 19 '23 at 16:27

0 Answers0