Skip to content

Commit 73d6144

Browse files
authored
BUG: Fix behavior of isocalendar with timezones (#33551)
1 parent ecc3b2e commit 73d6144

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/core/arrays/datetimes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,11 @@ def isocalendar(self):
12771277
"""
12781278
from pandas import DataFrame
12791279

1280-
sarray = fields.build_isocalendar_sarray(self.asi8)
1280+
if self.tz is not None and not timezones.is_utc(self.tz):
1281+
values = self._local_timestamps()
1282+
else:
1283+
values = self.asi8
1284+
sarray = fields.build_isocalendar_sarray(values)
12811285
iso_calendar_df = DataFrame(
12821286
sarray, columns=["year", "week", "day"], dtype="UInt32"
12831287
)

pandas/tests/indexes/datetimes/test_misc.py

+14
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,17 @@ def test_iter_readonly():
373373
arr.setflags(write=False)
374374
dti = pd.to_datetime(arr)
375375
list(dti)
376+
377+
378+
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
379+
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
380+
# return the same weekofyear accessor close to new year w/ tz
381+
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
382+
dates = DatetimeIndex(dates, tz="Europe/Brussels")
383+
result = dates.isocalendar()
384+
expected_data_frame = pd.DataFrame(
385+
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
386+
columns=["year", "week", "day"],
387+
dtype="UInt32",
388+
)
389+
tm.assert_frame_equal(result, expected_data_frame)

0 commit comments

Comments
 (0)