BUG: series.truncate doesn't return the correct value in pandas 1.1.0 · Issue #35544 · pandas-dev/pandas (original) (raw)


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.