Skip to content

ENH: Series between inclusive end point bifurcation #12398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
colinalexander opened this issue Feb 19, 2016 · 6 comments
Closed

ENH: Series between inclusive end point bifurcation #12398

colinalexander opened this issue Feb 19, 2016 · 6 comments

Comments

@colinalexander
Copy link

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).
@jreback
Copy link
Contributor

jreback commented Feb 20, 2016

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?

@colinalexander
Copy link
Author

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

@jorisvandenbossche
Copy link
Member

@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

@jreback
Copy link
Contributor

jreback commented Feb 21, 2016

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)

@shoyer
Copy link
Member

shoyer commented Feb 22, 2016

+1 for deprecating between

@jreback
Copy link
Contributor

jreback commented Apr 30, 2016

closed in favor of #13027

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants