Skip to content

Commit 74e8607

Browse files
authored
BUG: incorrect freq in PeriodIndex-Period (#33801)
1 parent f159734 commit 74e8607

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

pandas/core/indexes/datetimelike.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def wrapped(self, other):
8888
if result is NotImplemented:
8989
return NotImplemented
9090

91-
new_freq = self._get_addsub_freq(other)
91+
new_freq = self._get_addsub_freq(other, result)
9292
result._freq = new_freq
9393
return result
9494

@@ -451,14 +451,16 @@ def _partial_date_slice(
451451
# --------------------------------------------------------------------
452452
# Arithmetic Methods
453453

454-
def _get_addsub_freq(self, other) -> Optional[DateOffset]:
454+
def _get_addsub_freq(self, other, result) -> Optional[DateOffset]:
455455
"""
456456
Find the freq we expect the result of an addition/subtraction operation
457457
to have.
458458
"""
459459
if is_period_dtype(self.dtype):
460-
# Only used for ops that stay PeriodDtype
461-
return self.freq
460+
if is_period_dtype(result.dtype):
461+
# Only used for ops that stay PeriodDtype
462+
return self.freq
463+
return None
462464
elif self.freq is None:
463465
return None
464466
elif lib.is_scalar(other) and isna(other):

pandas/tests/arithmetic/test_period.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,13 @@ def test_pi_sub_period(self):
14441444
tm.assert_index_equal(result, exp)
14451445

14461446
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
1447-
tm.assert_index_equal(idx - pd.Period("NaT", freq="M"), exp)
1448-
tm.assert_index_equal(pd.Period("NaT", freq="M") - idx, exp)
1447+
result = idx - pd.Period("NaT", freq="M")
1448+
tm.assert_index_equal(result, exp)
1449+
assert result.freq == exp.freq
1450+
1451+
result = pd.Period("NaT", freq="M") - idx
1452+
tm.assert_index_equal(result, exp)
1453+
assert result.freq == exp.freq
14491454

14501455
def test_pi_sub_pdnat(self):
14511456
# GH#13071

0 commit comments

Comments
 (0)