Skip to content

ENH: Series between inclusive pair (GH: 12398) #12402

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ Other enhancements
- ``sys.getsizeof(obj)`` returns the memory usage of a pandas object, including the
values it contains (:issue:`11597`)
- ``Series`` gained an ``is_unique`` attribute (:issue:`11946`)
- ``Series`` method ``between`` now provides ability to set different inclusive endpoints, e.g. series.between(2, 5, inclusive=(True, False)). (:issue:`12398`)
- ``DataFrame.quantile`` and ``Series.quantile`` now accept ``interpolation`` keyword (:issue:`10174`).
- Added ``DataFrame.style.format`` for more flexible formatting of cell values (:issue:`11692`)
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
Expand Down
15 changes: 13 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,14 +2475,25 @@ def between(self, left, right, inclusive=True):
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:
lmask = self >= left
rmask = self <= right
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
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,11 @@ def test_between(self):
result = s[s.between(s[3], s[17], inclusive=False)]
expected = s[5:16].dropna()
assert_series_equal(result, expected)

result = s[s.between(s[3], s[17], inclusive=(True, False))]
expected = s[3:16].dropna()
assert_series_equal(result, expected)

result = s[s.between(s[3], s[17], inclusive=(False, True))]
expected = s[5:18].dropna()
assert_series_equal(result, expected)