ERR: better error message on non-sorted input with .truncate · Issue #17935 · pandas-dev/pandas (original) (raw)

@jreback

xref #17295

This is correct, but since the doc-string says sorted, we should check this and give a nicer message. (not that should check both .is_monotonic_increasing and decreasing cases, and confirm that the before/after match)

In [8]: rng2 = pd.date_range('2011-01-01', '2012-01-01', freq='W')
   ...: ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2)

In [9]: ts2.truncate(before='2011-11', after='2011-12')
Out[9]: 
2011-11-06    0.144986
2011-11-13   -1.823425
2011-11-20    1.742315
2011-11-27    0.311337
Freq: W-SUN, dtype: float64

In [10]: ts2.sort_values(ascending=False).truncate(before='2011-11', after='2011-12')
KeyError: Timestamp('2011-11-01 00:00:00')