Allow ability to distinguish inclusiveness of left and right boundaries. I've written the code and tests, but need a GH tracking number prior to submitting a PR.
def between(self, left, right, inclusive=True):
"""
Return boolean Series equivalent to left <= series <= right. NA values
will be treated as False
Parameters
----------
left : scalar
Left boundary
right : scalar
Right boundary
inclusive : boolean for both left and right or iterable pair.
Whether or not to include the left and right boundary.
If a tuple pair, the boundaries can be set separately
(e.g. (False, True) for left < series <= right).
Comment From: jreback
this should not be a separate method, maybe as an argument to .loc
. Though we already follow the conventions for positional indexing, and label indexing by definition includes the endpoints (as its a bit not-trivial not too).
So what exactly is the use case here?
Comment From: colinalexander
Desired functionality is to augment existing functionality of pd.Series.between as follows:
Instead of this...
s = pd.Series(range(5))
>>> s.loc[(s > 2) & (s <= 4)]
3 3
4 4
dtype: int64
Have something like this:
>>> s.between(2, 4, inclusive=(False, True))
3 3
4 4
dtype: int64
Here is the first proposed revision:
def between(self, left, right, inclusive=True):
"""
Return boolean Series equivalent to left <= series <= right. NA values
will be treated as False
Parameters
----------
left : scalar
Left boundary
right : scalar
Right boundary
inclusive : Boolean to indicate whether or not to include both the left
and right endpoints (True: a <= series <= b, False: a < series < b),
or an iterable boolean pair to set them separately, e.g
inclusive= (False, True) for a < series <= b.
Returns
-------
is_between : Series
"""
if inclusive:
try:
pair = iter(inclusive)
left_inclusive = pair.next()
rigt_inclusive = pair.next()
lmask = self >= left if left_inclusive else self > left
rmask = self <= right if rigt_inclusive else self < right
except TypeError:
lmask = self >= left
rmask = self <= right
else:
lmask = self > left
rmask = self < right
return lmask & rmask
Comment From: jorisvandenbossche
@jreback yes, this method already exists ... (didn't know that! so my first reaction was the same as yours)
So given that, I don't see any harm is extending its functionality a little bit
Comment From: jreback
I suppose ok, but let's deprecate this method entirely. This is just another (confusing) way of doing things. There are already too many ways of doing indexing. This (although a nice name) is just bloating the API.
Pls create another issue for this deprecation (which will have to wait for 0.19.0)
Comment From: shoyer
+1 for deprecating between
Comment From: jreback
closed in favor of #13027