Fix: _dts_in_same_interval("1mo") ignored year, treating same-month in different years as the same interval
The "1mo" branch compared only `dt1.month == dt2.month`, so e.g. (2024-12-15, 2025-12-15) returned True. The "3mo" branch already handles year differences via `year_diff`; mirror that by also requiring `dt1.year == dt2.year`. Added a test (`test_same_month_different_year`) that fails on the old logic and passes on the fix. All existing tests still pass.
This commit is contained in:
@@ -104,6 +104,17 @@ class TestDateIntervalCheck(unittest.TestCase):
|
||||
dt2 = pd.Timestamp("2025-01-15")
|
||||
self.assertFalse(_dts_in_same_interval(dt1, dt2, "1mo"))
|
||||
|
||||
def test_same_month_different_year(self):
|
||||
# Same calendar month but different years must not be treated
|
||||
# as the same monthly interval.
|
||||
dt1 = pd.Timestamp("2024-12-15")
|
||||
dt2 = pd.Timestamp("2025-12-15")
|
||||
self.assertFalse(_dts_in_same_interval(dt1, dt2, "1mo"))
|
||||
|
||||
dt3 = pd.Timestamp("2020-06-01")
|
||||
dt4 = pd.Timestamp("2025-06-01")
|
||||
self.assertFalse(_dts_in_same_interval(dt3, dt4, "1mo"))
|
||||
|
||||
def test_standard_quarters(self):
|
||||
q1_start = datetime(2023, 1, 1)
|
||||
self.assertTrue(_dts_in_same_interval(q1_start, datetime(2023, 1, 15), '3mo'))
|
||||
|
||||
+1
-1
@@ -624,7 +624,7 @@ def _dts_in_same_interval(dt1, dt2, interval):
|
||||
elif interval == "1wk":
|
||||
last_rows_same_interval = (dt2 - dt1).days < 7
|
||||
elif interval == "1mo":
|
||||
last_rows_same_interval = dt1.month == dt2.month
|
||||
last_rows_same_interval = dt1.month == dt2.month and dt1.year == dt2.year
|
||||
elif interval == "3mo":
|
||||
shift = (dt1.month % 3) - 1
|
||||
q1 = (dt1.month - shift - 1) // 3 + 1
|
||||
|
||||
Reference in New Issue
Block a user