[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    [Python-ideas] Re: PEP 472 - slices in keyword indices, d[x=1:3]
From:       Todd <toddrjen () gmail ! com>
Date:       2020-08-26 1:31:12
Message-ID: CAFpSVpKDzBj6mVGWyC5LHTYF1mKGws=6F67svAxv5bFYwq1v3g () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Tue, Aug 25, 2020 at 10:58 AM David Mertz <mertz@gnosis.cx> wrote:

> On Tue, Aug 25, 2020 at 2:26 AM Christopher Barker <pythonchb@gmail.com>
> wrote:
>
>> As for "why not" not being a motivator -- I agree, I posted it that easy
>> because this conversation has brought up a number of examples where slice
>> syntax is nice to use. And David Mertz pointed out, both numpy and pandas
>> have a utility to make easier slices -- yes, that's an argument for why you
>> don't need them, but it's also an argument for why there IS a need for
>> slice objects outside of the square brackets, and if we need slice objects,
>> then slice syntax is nice.
>>
>
> Pandas is kinda special though.  It semi-abuses Python syntax in quite a
> few places.  For example, here is an example from the Pandas docs:
>
> >>> idx = pd.IndexSlice>>> dfmi.loc[idx[:, 'B0':'B1'], :]
>
> There is a hierarchical index (MultiIndex) where we want to put slices as
> some of the "dimensions" of slice items.  It definitely makes sense in the
> Pandas world, but I have never once encountered anywhere I would want
> "stand-alone" slice objects outside of Pandas.  I know NumPy includes the
> same convenience, but it's not even coming to me immediately in what
> context you would want that in NumPy.
>

I had to do it in a situation where I needed slices but couldn't tell ahead
of time how many dimensions would be sliced.  So I had to construct a
dynamically-sized tuple of slices on-the-fly.  I doubt this is not a common
situation, though.

[Attachment #5 (text/html)]

<div dir="ltr"><div dir="ltr">On Tue, Aug 25, 2020 at 10:58 AM David Mertz &lt;<a \
href="mailto:mertz@gnosis.cx">mertz@gnosis.cx</a>&gt; wrote:<br></div><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">On Tue, \
Aug 25, 2020 at 2:26 AM Christopher Barker &lt;<a href="mailto:pythonchb@gmail.com" \
target="_blank">pythonchb@gmail.com</a>&gt; wrote:<br><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">As for \
&quot;why not&quot; not being a motivator -- I agree, I posted it that easy because \
this conversation has brought up a number of examples where slice syntax is nice to \
use. And David Mertz pointed out, both numpy and pandas have a utility to make easier \
slices -- yes, that&#39;s an argument for why you don&#39;t need them, but it&#39;s \
also an argument for why there IS a need for slice objects outside of the square \
brackets, and if we need slice objects, then slice syntax is \
nice.</div></blockquote><div><br></div><div>Pandas is kinda special though.   It \
semi-abuses Python syntax in quite a few places.   For example, here is an example \
from the Pandas docs:</div><div><pre><span>&gt;&gt;&gt; </span><span>idx</span> \
<span>=</span> <span>pd</span><span>.</span><span>IndexSlice</span> \
<span>&gt;&gt;&gt; </span><span>dfmi</span><span>.</span><span>loc</span><span>[</span><span>idx</span><span>[:,</span> \
<span>&#39;B0&#39;</span><span>:</span><span>&#39;B1&#39;</span><span>],</span> \
<span>:]</span></pre></div><div>There is a hierarchical index (MultiIndex) where we \
want to put slices as some of the &quot;dimensions&quot; of slice items.   It \
definitely makes sense in the Pandas world, but I have never once encountered \
anywhere I would want &quot;stand-alone&quot; slice objects outside of Pandas.   I \
know NumPy includes the same convenience, but it&#39;s not even coming to me \
immediately in what context you would want that in \
NumPy.</div></div></div></blockquote><div><br></div><div>I had to do it in a \
situation where I needed slices but couldn&#39;t tell ahead of time how many \
dimensions would be sliced.   So I had to construct a dynamically-sized tuple of \
slices on-the-fly.   I doubt this is not a common situation, though.  \
</div></div></div>



_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/EGEOV6YLGH7IWEWQDFLXPDF3UPQ3UEGS/
 Code of Conduct: http://python.org/psf/codeofconduct/



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic