Skip to content

Commit 6c1ddc9

Browse files
committed
BUG: Pandas can't restore index from parquet with offset-specified timezone #35997
1 parent c688a0f commit 6c1ddc9

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

pandas/_libs/tslibs/timezones.pyx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import timezone
1+
from datetime import timedelta, timezone
22

33
from cpython.datetime cimport datetime, timedelta, tzinfo
44

@@ -102,6 +102,14 @@ cpdef inline tzinfo maybe_get_tz(object tz):
102102
# On Python 3 on Windows, the filename is not always set correctly.
103103
if isinstance(tz, _dateutil_tzfile) and '.tar.gz' in tz._filename:
104104
tz._filename = zone
105+
elif tz[0] in {'-', '+'}:
106+
hours = int(tz[0:3])
107+
minutes = int(tz[0] + tz[4:6])
108+
tz = timezone(timedelta(hours=hours, minutes=minutes))
109+
elif tz[0:4] in {'UTC-', 'UTC+'}:
110+
hours = int(tz[3:6])
111+
minutes = int(tz[3] + tz[7:9])
112+
tz = timezone(timedelta(hours=hours, minutes=minutes))
105113
else:
106114
tz = pytz.timezone(tz)
107115
elif is_integer_object(tz):

pandas/conftest.py

+4
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,10 @@ def iris(datapath):
837837
"Asia/Tokyo",
838838
"dateutil/US/Pacific",
839839
"dateutil/Asia/Singapore",
840+
"+01:15",
841+
"-02:15",
842+
"UTC+01:15",
843+
"UTC-02:15",
840844
tzutc(),
841845
tzlocal(),
842846
FixedOffset(300),

pandas/tests/io/test_parquet.py

+31
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ def df_full():
123123
)
124124

125125

126+
@pytest.fixture(
127+
params=[
128+
datetime.datetime.now(datetime.timezone.utc),
129+
datetime.datetime.now(datetime.timezone.min),
130+
datetime.datetime.now(datetime.timezone.max),
131+
datetime.datetime.strptime("2019-01-04T16:41:24+0200", "%Y-%m-%dT%H:%M:%S%z"),
132+
datetime.datetime.strptime("2019-01-04T16:41:24+0215", "%Y-%m-%dT%H:%M:%S%z"),
133+
datetime.datetime.strptime("2019-01-04T16:41:24-0200", "%Y-%m-%dT%H:%M:%S%z"),
134+
datetime.datetime.strptime("2019-01-04T16:41:24-0215", "%Y-%m-%dT%H:%M:%S%z"),
135+
]
136+
)
137+
def timezone_aware_date_list(request):
138+
return request.param
139+
140+
126141
def check_round_trip(
127142
df,
128143
engine=None,
@@ -712,6 +727,13 @@ def test_timestamp_nanoseconds(self, pa):
712727
df = pd.DataFrame({"a": pd.date_range("2017-01-01", freq="1n", periods=10)})
713728
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
714729

730+
@td.skip_if_no("pyarrow", min_version="0.14")
731+
def test_timezone_aware_index(self, pa, timezone_aware_date_list):
732+
idx = 5 * [timezone_aware_date_list]
733+
734+
df = pd.DataFrame(index=idx)
735+
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
736+
715737
@td.skip_if_no("pyarrow", min_version="0.17")
716738
def test_filter_row_groups(self, pa):
717739
# https://github.com/pandas-dev/pandas/issues/26551
@@ -850,3 +872,12 @@ def test_empty_dataframe(self, fp):
850872
expected = df.copy()
851873
expected.index.name = "index"
852874
check_round_trip(df, fp, expected=expected)
875+
876+
def test_timezone_aware_index(self, fp, timezone_aware_date_list):
877+
idx = 5 * [timezone_aware_date_list]
878+
879+
df = pd.DataFrame(index=idx, data={"col": 5 * [0]})
880+
881+
expected = df.copy()
882+
expected.index.name = "index"
883+
check_round_trip(df, fp, expected=expected)

0 commit comments

Comments
 (0)