From 41c8dae9770571213a456b57125956d59d6edb43 Mon Sep 17 00:00:00 2001 From: Neel Raman Date: Sat, 3 Jul 2021 23:09:33 -0400 Subject: [PATCH 1/2] BUG: truncate has incorrect behavior when index has only one unique value (#42365) --- pandas/core/generic.py | 2 +- pandas/tests/frame/methods/test_truncate.py | 10 ++++++++++ pandas/tests/series/methods/test_truncate.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1abb01099f977..7ea1f0c6216a4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9381,7 +9381,7 @@ def truncate( if before is not None and after is not None and before > after: raise ValueError(f"Truncate: {after} must be after {before}") - if len(ax) > 1 and ax.is_monotonic_decreasing: + if len(ax) > 1 and ax.is_monotonic_decreasing and ax.nunique() > 1: before, after = after, before slicer = [slice(None, None)] * self._AXIS_LEN diff --git a/pandas/tests/frame/methods/test_truncate.py b/pandas/tests/frame/methods/test_truncate.py index 210e86067566a..f2d60240009c5 100644 --- a/pandas/tests/frame/methods/test_truncate.py +++ b/pandas/tests/frame/methods/test_truncate.py @@ -148,3 +148,13 @@ def test_truncate_multiindex(self, frame_or_series): expected = expected["col"] tm.assert_equal(result, expected) + + def test_truncate_index_only_one_unique_value(self, frame_or_series): + # GH 42365 + obj = Series(0, index=date_range("2021-06-30", "2021-06-30")).repeat(5) + if frame_or_series is DataFrame: + obj = obj.to_frame(name="a") + + truncated = obj.truncate("2021-06-28", "2021-07-01") + + tm.assert_equal(truncated, obj) diff --git a/pandas/tests/series/methods/test_truncate.py b/pandas/tests/series/methods/test_truncate.py index ca5c3e2639097..a3a27a744b180 100644 --- a/pandas/tests/series/methods/test_truncate.py +++ b/pandas/tests/series/methods/test_truncate.py @@ -55,3 +55,11 @@ def test_truncate_one_element_series(self): # the input Series and the expected Series are the same tm.assert_series_equal(result, series) + + def test_truncate_index_only_one_unique_value(self): + # GH 42365 + obj = Series(0, index=date_range("2021-06-30", "2021-06-30")).repeat(5) + + truncated = obj.truncate("2021-06-28", "2021-07-01") + + tm.assert_series_equal(truncated, obj) From b7d5ad7525785edbc4b79ae653e26db52de3ec83 Mon Sep 17 00:00:00 2001 From: Neel Raman Date: Sun, 4 Jul 2021 15:04:17 -0400 Subject: [PATCH 2/2] add whatsnew entry for #42365 --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 24f307f23f435..ba52385ca24a9 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -159,7 +159,7 @@ Interval Indexing ^^^^^^^^ - Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` when passing a string, the return type depended on whether the index was monotonic (:issue:`24892`) -- +- Bug in :meth:`DataFrame.truncate` and :meth:`Series.truncate` when the object's Index has a length greater than one but only one unique value (:issue:`42365`) Missing ^^^^^^^