From e7949765eb935196b61105c34ad4c4ef7eb8afbf Mon Sep 17 00:00:00 2001 From: JosieGarba Date: Tue, 18 Apr 2023 19:19:21 -0400 Subject: [PATCH] changes --- pandas/core/generic.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 582a043a8a78a..77ebcb4b3fed9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9145,9 +9145,19 @@ def last(self, offset) -> Self: offset = to_offset(offset) - start_date = self.index[-1] - offset - start = self.index.searchsorted(start_date, side="right") - return self.iloc[start:] + if not isinstance(offset, Tick) and offset.is_on_offset(self.index[-1]): + # GH#29623 if first value is end of period, remove offset with n = 1 + # before adding the real offset + start_date = start = self.index[-1] - offset.base - offset + else: + start_date = start = self.index[-1] - offset + + # Tick-like, e.g. 3 weeks + if isinstance(offset, Tick) and start_date in self.index: + start = self.index.searchsorted(start_date, side="right") + return self.iloc[:start] + + return self.loc[:start] @final def rank(