Skip to content

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

Merged
merged 5 commits into from
Aug 7, 2020
Merged

Bug fix one element series truncate #35547

merged 5 commits into from
Aug 7, 2020

Conversation

gabicca
Copy link
Contributor

@gabicca gabicca commented Aug 4, 2020

Fix when trying to truncate a one-element series within a correct date range returned an empty series instead of the originally passed-in series.

@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think checking whether a single element series is "increasing" or "decreasing" really makes sense

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, misunderstood your point. I'll take a look at that code.

Copy link
Contributor

@TomAugspurger TomAugspurger Aug 4, 2020

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

After digging a bit, I think you're right, though couldn't find a really good reference... Do you maybe have one?

@WillAyd WillAyd added the Series Series data structure label Aug 4, 2020
@@ -49,7 +49,7 @@ Categorical

**Indexing**

-
- Bug in series.truncate when trying to truncate a single-element series (:issue:`35544`)
Copy link
Contributor

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`

Copy link
Contributor Author

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.

before = pd.Timestamp("2020-08-02")
after = pd.Timestamp("2020-08-04")

assert series.truncate(before=before, after=after).equals(series)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use

result = 
expected = 
tm.assert_series_equal(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@simonjayhawkins simonjayhawkins added this to the 1.1.1 milestone Aug 5, 2020
@jreback jreback added Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version labels Aug 6, 2020
@jreback
Copy link
Contributor

jreback commented Aug 6, 2020

ok looks good, can you merge master and ping on green.

@gabicca
Copy link
Contributor Author

gabicca commented Aug 7, 2020

ok looks good, can you merge master and ping on green.

@jreback All green!

@jreback jreback merged commit 63a48bb into pandas-dev:master Aug 7, 2020
@jreback
Copy link
Contributor

jreback commented Aug 7, 2020

thanks @gabicca

@gabicca
Copy link
Contributor Author

gabicca commented Aug 7, 2020

thanks @gabicca

Awesome, Thanks!

@gabicca gabicca deleted the bug-fix-one-element-series-truncate branch August 7, 2020 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version Series Series data structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: series.truncate doesn't return the correct value in pandas 1.1.0
5 participants