-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix interval constructor to handle left=right case #26894
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
Changes from 6 commits
023c0ad
d133fd4
7234906
df50958
96a297a
e4e8632
b0cd7a3
01ff298
b3d18ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,9 +476,14 @@ def _format_labels(bins, precision, right=True, | |
if right and include_lowest: | ||
# we will adjust the left hand side by precision to | ||
# account that we are all right closed | ||
v = adjust(labels[0].left) | ||
|
||
i = IntervalIndex([Interval(v, labels[0].right, closed='right')]) | ||
# | ||
# We cannot access labels[0].left because that | ||
# calls Interval(left, right, closed='right') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not clear what the issue is here, can you show a full example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment explains it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
# and before adjusting we may have left == right | ||
# so it will raise. | ||
v = adjust(labels.left[0]) | ||
|
||
i = IntervalIndex([Interval(v, labels.right[0], closed='right')]) | ||
labels = i.append(labels[1:]) | ||
|
||
return labels | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,23 @@ def test_repr_matches(): | |
a = repr(idx) | ||
b = repr(idx.values) | ||
assert a.replace("Index", "Array") == b | ||
|
||
|
||
def test_point_interval(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make the error conditions as a separate parmeterized test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
match = "both/neither sides must be closed when left == right" | ||
for closed in ('left', 'right'): | ||
with pytest.raises(ValueError, match=match): | ||
pd.Interval(0, 0, closed) | ||
|
||
pd.Interval(0, 0, 'neither') # no exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assert the result value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pd.Interval(0, 0, 'both') # no exception | ||
|
||
assert not pd.Interval(0, 1, "left").overlaps(pd.Interval(0, 0, "neither")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would prefer this is a paramterized test as this is pretty hard to read this way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
assert pd.Interval(0, 1, "left").overlaps(pd.Interval(0, 0, "both")) | ||
assert not pd.Interval(0, 1, "right").overlaps( | ||
pd.Interval(1, 1, "neither")) | ||
assert pd.Interval(0, 1, "right").overlaps(pd.Interval(1, 1, "both")) | ||
assert not pd.Interval(0, 1, "both").overlaps(pd.Interval(0, 0, "neither")) | ||
assert not pd.Interval(0, 1, "both").overlaps(pd.Interval(0, 0, "neither")) | ||
assert not pd.Interval(0, 1, "neither").overlaps(pd.Interval(1, 1, "both")) | ||
assert not pd.Interval(0, 1, "neither").overlaps(pd.Interval(1, 1, "both")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a very friendly comment, can you re-word, put double backticks around ``closed=`. This is also not clear what you are actually fixing here. What is an inconsistent 'closed' value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm talking about
(0,0]
or[0,0)
when the same point is both open and closed, which makes no sense. Can you suggest a better wording?closed
is a kwarg.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded with explicit cases