
In [3]: u = np.arange(10) In [4]: u Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) In [5]: u[-2:] Out[5]: array([8, 9]) In [6]: u[-2:2] Out[6]: array([], dtype=int64) I would argue for consistency it would be desirable for this to return [8, 9, 0, 1]

On Thu, Jun 7, 2012 at 7:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
In [3]: u = np.arange(10)
In [4]: u Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [5]: u[-2:] Out[5]: array([8, 9])
In [6]: u[-2:2] Out[6]: array([], dtype=int64)
I would argue for consistency it would be desirable for this to return
[8, 9, 0, 1]
Unfortunately, this would be inconsistent with Python semantics: [~] |1> u = range(10) [~] |2> u[-2:2] [] -- Robert Kern

Robert Kern wrote:
On Thu, Jun 7, 2012 at 7:55 PM, Neal Becker <ndbecker2@gmail.com> wrote:
In [3]: u = np.arange(10)
In [4]: u Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [5]: u[-2:] Out[5]: array([8, 9])
In [6]: u[-2:2] Out[6]: array([], dtype=int64)
I would argue for consistency it would be desirable for this to return
[8, 9, 0, 1]
Unfortunately, this would be inconsistent with Python semantics:
[~] |1> u = range(10)
[~] |2> u[-2:2] []
The fact that this proposed numpy behavior would not match python list behavior holds little weight for me. I would still favor this change, unless it added significant overhead. My opinion, of course.

On 08/06/12 14:14, Neal Becker wrote:
The fact that this proposed numpy behavior would not match python list behavior holds little weight for me. I would still favor this change, unless it added significant overhead. My opinion, of course.
It holds enormous weight for me. My opinion is that NumPy arrays should be consistent with Python sequences as much as possible.

On 08/06/12 14:14, Neal Becker wrote:
The fact that this proposed numpy behavior would not match python list behavior holds little weight for me. I would still favor this change, unless it added significant overhead. My opinion, of course.
As a "Joe User", I think using the [-2:2] syntax would be a mistake, though I agree it would be a useful shortcut (e.g. FFTs or power series where you need to wrap around). My concern is this: in a multiple dimension case, which are the -2 and -1 that you really want? Example, in a 2-D image that is constructed by reshaping a series of samples ordered in time, there are occasions where I would want the first two and last two samples of the same row (looking at spatial effects), and other times where I want the right two samples of the preceding row and the left two samples of the next row (looking at time effects). Which is the correct interpretation? It may be obvious to most people that it is "from the same row", but that isn't so obvious to me. Actually, my own preference would be that this construction throws an exception as a malformed slice, and I really wonder why python itself doesn't do this. Mike -- mike.ressler@alum.mit.edu

On 06/07/2012 12:55 PM, Neal Becker wrote:
In [3]: u = np.arange(10)
In [4]: u Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [5]: u[-2:] Out[5]: array([8, 9])
In [6]: u[-2:2] Out[6]: array([], dtype=int64)
I would argue for consistency it would be desirable for this to return
[8, 9, 0, 1]
Should u[8:2] also return [8, 9, 0, 1], for consistency? That would be the concatenation of u[8:] and u[:2], which seems to be your argument. I concur with Alan that a numpy array should, to the extent possible, remain a sequence type. It's a pretty good duck without a peacock tail ;) -- Jonathan Niehof ISR-3 Space Data Systems Los Alamos National Laboratory MS-D466 Los Alamos, NM 87545 Phone: 505-667-9595 email: jniehof@lanl.gov Correspondence / Technical data or Software Publicly Available
participants (6)
-
Alan G Isaac
-
Bob Dowling
-
Jonathan T. Niehof
-
Mike Ressler
-
Neal Becker
-
Robert Kern