Skip to content

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

Closed
@gabicca

Description

@gabicca
  • [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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions