Skip to content

Commit 45bf911

Browse files
Backport PR pandas-dev#36050: BUG: incorrect year returned in isocalendar for certain dates (pandas-dev#36127)
Co-authored-by: Asish Mahapatra <[email protected]>
1 parent 820218d commit 45bf911

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bug fixes
3333
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
3434
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should be ``""`` (:issue:`35712`)
3535
- Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`)
36+
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
3637
- Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`33675`)
3738

3839
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/ccalendar.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:
201201
iso_week = 1
202202

203203
iso_year = year
204-
if iso_week == 1 and doy > 7:
204+
if iso_week == 1 and month == 12:
205205
iso_year += 1
206206

207-
elif iso_week >= 52 and doy < 7:
207+
elif iso_week >= 52 and month == 1:
208208
iso_year -= 1
209209

210210
return iso_year, iso_week, dow + 1

pandas/tests/series/test_datetime_values.py

+3
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ def test_setitem_with_different_tz(self):
682682
[[pd.NaT], [[np.NaN, np.NaN, np.NaN]]],
683683
[["2019-12-31", "2019-12-29"], [[2020, 1, 2], [2019, 52, 7]]],
684684
[["2010-01-01", pd.NaT], [[2009, 53, 5], [np.NaN, np.NaN, np.NaN]]],
685+
# see GH#36032
686+
[["2016-01-08", "2016-01-04"], [[2016, 1, 5], [2016, 1, 1]]],
687+
[["2016-01-07", "2016-01-01"], [[2016, 1, 4], [2015, 53, 5]]],
685688
],
686689
)
687690
def test_isocalendar(self, input_series, expected_output):

pandas/tests/tslibs/test_ccalendar.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from datetime import date, datetime
22

3+
from hypothesis import given, strategies as st
34
import numpy as np
45
import pytest
56

67
from pandas._libs.tslibs import ccalendar
78

9+
import pandas as pd
10+
811

912
@pytest.mark.parametrize(
1013
"date_tuple,expected",
@@ -48,3 +51,15 @@ def test_dt_correct_iso_8601_year_week_and_day(input_date_tuple, expected_iso_tu
4851
expected_from_date_isocalendar = date(*input_date_tuple).isocalendar()
4952
assert result == expected_from_date_isocalendar
5053
assert result == expected_iso_tuple
54+
55+
56+
@given(
57+
st.datetimes(
58+
min_value=pd.Timestamp.min.to_pydatetime(warn=False),
59+
max_value=pd.Timestamp.max.to_pydatetime(warn=False),
60+
)
61+
)
62+
def test_isocalendar(dt):
63+
expected = dt.isocalendar()
64+
result = ccalendar.get_iso_calendar(dt.year, dt.month, dt.day)
65+
assert result == expected

0 commit comments

Comments
 (0)