Skip to content

Backport PR #35547 on branch 1.1.x (Bug fix one element series truncate) #35600

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -9397,7 +9397,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:
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)