Skip to content
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Categorical

**Indexing**

-
- Bug in :meth:`Series.truncate` when trying to truncate a single-element series (:issue:`35544`)

**DataFrame**
- Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when data and index have mismatched lengths (:issue:`33437`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9405,7 +9405,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?

before, after = after, before

slicer = [slice(None, None)] * self._AXIS_LEN
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,14 @@ def test_truncate_multiindex(self):
expected = df.col

tm.assert_series_equal(result, expected)

def test_truncate_one_element_series(self):
# GH 35544
series = pd.Series([0.1], index=pd.DatetimeIndex(["2020-08-04"]))
before = pd.Timestamp("2020-08-02")
after = pd.Timestamp("2020-08-04")

result = series.truncate(before=before, after=after)

# the input Series and the expected Series are the same
tm.assert_series_equal(result, series)