-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Bug fix one element series truncate #35547
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 3 commits
8f91dd0
a72a62b
f667212
2e85aef
215e65d
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 |
---|---|---|
|
@@ -9397,7 +9397,7 @@ def truncate( | |
if before > after: | ||
raise ValueError(f"Truncate: {after} must be after {before}") | ||
|
||
if ax.is_monotonic_decreasing: | ||
if len(ax) > 1 and ax.is_monotonic_decreasing: | ||
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. Is there a reason why a single element container should be monotonic_decrasing? Wondering if the real issue isn't that property 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. Both .is_monotonic_decreasing and .is_monotonic_increasing will return true for a single-element series. I don't think checking whether a single element series is "increasing" or "decreasing" really makes sense, so this fix really just prevents the code from checking. The fix was suggested by @TomAugspurger , and the test is passing after the fix. 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.
Yea that's my point. Instead of special casing this to check the length if the property returned False you would get the same result 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. OK, misunderstood your point. I'll take a look at that code. 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 think length-1 (and probably length-0) sequences are both monotonically increasing and decreasing by definition. 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.
After digging a bit, I think you're right, though couldn't find a really good reference... Do you maybe have one? |
||
before, after = after, before | ||
|
||
slicer = [slice(None, None)] * self._AXIS_LEN | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,3 +141,11 @@ def test_truncate_multiindex(self): | |
expected = df.col | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
def test_truncate_one_element_series(self): | ||
# GH 35544 | ||
series = pd.Series([0.1], index=pd.DatetimeIndex(["2020-08-04"])) | ||
before = pd.Timestamp("2020-08-02") | ||
after = pd.Timestamp("2020-08-04") | ||
|
||
assert series.truncate(before=before, after=after).equals(series) | ||
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. can you use
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 updated this one too. The input and expected series are the same. I added a comment to clarify this. |
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.
should be
:meth:`Series.truncate`
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.
Thanks for the review. This has been updated.