Skip to content

BUG: series.truncate doesn't return the correct value in pandas 1.1.0 #35544

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
1 task
gabicca opened this issue Aug 4, 2020 · 2 comments · Fixed by #35547
Closed
1 task

BUG: series.truncate doesn't return the correct value in pandas 1.1.0 #35544

gabicca opened this issue Aug 4, 2020 · 2 comments · Fixed by #35547
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@gabicca
Copy link
Contributor

gabicca commented Aug 4, 2020

  • [Y] I have checked that this issue has not already been reported.

  • [Y] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

def test_truncate():
    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)

Problem description

The above test passes in pandas 1.0.5 while fails in pandas 1.1.0.

After diving into the code I found that truncate in pandas/core/generic.py changed as follows:

if ax.is_monotonic_decreasing:
     before, after = after, before

the above check was added to the method. ax.is_monotonic_decreasing returns true for 1-element series, (ax.is_monotonic_increasing also returns true). When before, and after are given in the correct, increasing order (like in the test), the following check reverses them and the result of truncate will be an empty series, instead of returning the input series which is the expected behaviour.

@gabicca gabicca added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2020
@TomAugspurger
Copy link
Contributor

Thanks for the report and clear diagnosis.

Are you interested in submitting a pull request with a fix? I think a check that len(ax) > 1 could be added before flipping the before and after.

@TomAugspurger TomAugspurger added Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2020
@TomAugspurger TomAugspurger added this to the 1.1.1 milestone Aug 4, 2020
@TomAugspurger TomAugspurger added the Indexing Related to indexing on series/frames, not to indexes themselves label Aug 4, 2020
@gabicca
Copy link
Contributor Author

gabicca commented Aug 4, 2020

Thanks for the report and clear diagnosis.

Are you interested in submitting a pull request with a fix? I think a check that len(ax) > 1 could be added before flipping the before and after.

Thanks! Sure, I'll take a closer look and submit a PR with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants