[Python-ideas] str(slice(10)) should return "slice(10)"

Stephan Hoyer shoyer at gmail.com
Sat Nov 12 20:13:58 EST 2016


On Sat, Nov 12, 2016 at 11:10 AM, David Mertz <mertz at gnosis.cx> wrote:

> +100
>
> I like this idea of giving `slice` a metaclass that defines a
> `.__getitem__()` allowing us to construct slices on the slice type itself.
>
> FWIW, this is exactly what pandas.IndexSlice does.  E.g., from
> http://pandas.pydata.org/pandas-docs/stable/advanced.html:
>

Indeed, this would be really nice! The requirement to use a special object
to construct slices outside of indexing is a repeated pain-point for me and
users of pandas/xarray.

To non-experts, it's not at all obvious what slice(None, 3, None) means,
but slice[:3] has the familiar syntax.

In xarray, we encourage passing around slice objects to do indexing with
keyword arguments [1], e.g., data.sel(time=slice(100)) to pull out the
first 100 values along the time axis. data.sel(time=slice[:100]) would be a
significant improvement.

Even if we ever get indexing with keyword arguments in Python, I still like
the readability of data[time=slice[:100]] better than data[time=:100],
where the colon gets lost.

[1]
http://xarray.pydata.org/en/stable/indexing.html#indexing-with-labeled-dimensions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/049fa382/attachment.html>


More information about the Python-ideas mailing list