Skip to content

Commit 7a6aefe

Browse files
gabiccameeseeksmachine
authored andcommitted
Backport PR pandas-dev#35547: Bug fix one element series truncate
1 parent 5904807 commit 7a6aefe

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v1.1.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Categorical
4848

4949
**Indexing**
5050

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

5353
**DataFrame**
5454
- Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when data and index have mismatched lengths (:issue:`33437`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9397,7 +9397,7 @@ def truncate(
93979397
if before > after:
93989398
raise ValueError(f"Truncate: {after} must be after {before}")
93999399

9400-
if ax.is_monotonic_decreasing:
9400+
if len(ax) > 1 and ax.is_monotonic_decreasing:
94019401
before, after = after, before
94029402

94039403
slicer = [slice(None, None)] * self._AXIS_LEN

pandas/tests/series/methods/test_truncate.py

+11
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,14 @@ def test_truncate_multiindex(self):
141141
expected = df.col
142142

143143
tm.assert_series_equal(result, expected)
144+
145+
def test_truncate_one_element_series(self):
146+
# GH 35544
147+
series = pd.Series([0.1], index=pd.DatetimeIndex(["2020-08-04"]))
148+
before = pd.Timestamp("2020-08-02")
149+
after = pd.Timestamp("2020-08-04")
150+
151+
result = series.truncate(before=before, after=after)
152+
153+
# the input Series and the expected Series are the same
154+
tm.assert_series_equal(result, series)

0 commit comments

Comments
 (0)