From 6313b2d9c6e6fc40408bf624d8fd7b1d8a2eb2ee Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 25 Apr 2020 19:07:49 -0700 Subject: [PATCH] BUG: incorrect freq in PeriodIndex-Period --- pandas/core/indexes/datetimelike.py | 10 ++++++---- pandas/tests/arithmetic/test_period.py | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 4ddac12ae8155..f41044db2b49c 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -88,7 +88,7 @@ def wrapped(self, other): if result is NotImplemented: return NotImplemented - new_freq = self._get_addsub_freq(other) + new_freq = self._get_addsub_freq(other, result) result._freq = new_freq return result @@ -451,14 +451,16 @@ def _partial_date_slice( # -------------------------------------------------------------------- # Arithmetic Methods - def _get_addsub_freq(self, other) -> Optional[DateOffset]: + def _get_addsub_freq(self, other, result) -> Optional[DateOffset]: """ Find the freq we expect the result of an addition/subtraction operation to have. """ if is_period_dtype(self.dtype): - # Only used for ops that stay PeriodDtype - return self.freq + if is_period_dtype(result.dtype): + # Only used for ops that stay PeriodDtype + return self.freq + return None elif self.freq is None: return None elif lib.is_scalar(other) and isna(other): diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 4cf1988a33de1..55a547b361eb3 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -1444,8 +1444,13 @@ def test_pi_sub_period(self): tm.assert_index_equal(result, exp) exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx") - tm.assert_index_equal(idx - pd.Period("NaT", freq="M"), exp) - tm.assert_index_equal(pd.Period("NaT", freq="M") - idx, exp) + result = idx - pd.Period("NaT", freq="M") + tm.assert_index_equal(result, exp) + assert result.freq == exp.freq + + result = pd.Period("NaT", freq="M") - idx + tm.assert_index_equal(result, exp) + assert result.freq == exp.freq def test_pi_sub_pdnat(self): # GH#13071