Skip to content

Commit 94c0763

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

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-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/tests/io/test_parquet.py

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

125125

126+
@pytest.fixture(
127+
params=[
128+
datetime.datetime.now(datetime.timezone.utc),
129+
datetime.datetime.strptime("2019-01-04T16:41:24+0200", "%Y-%m-%dT%H:%M:%S%z"),
130+
datetime.datetime.strptime("2019-01-04T16:41:24+0215", "%Y-%m-%dT%H:%M:%S%z"),
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+
]
134+
)
135+
def timezone_aware_date_list(request):
136+
return request.param
137+
138+
126139
def check_round_trip(
127140
df,
128141
engine=None,
@@ -712,6 +725,13 @@ def test_timestamp_nanoseconds(self, pa):
712725
df = pd.DataFrame({"a": pd.date_range("2017-01-01", freq="1n", periods=10)})
713726
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
714727

728+
@td.skip_if_no("pyarrow", min_version="0.14")
729+
def test_timezone_aware_index(self, pa, timezone_aware_date_list):
730+
idx = 5 * [timezone_aware_date_list]
731+
732+
df = pd.DataFrame(index=idx)
733+
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
734+
715735
@td.skip_if_no("pyarrow", min_version="0.17")
716736
def test_filter_row_groups(self, pa):
717737
# https://github.com/pandas-dev/pandas/issues/26551
@@ -850,3 +870,12 @@ def test_empty_dataframe(self, fp):
850870
expected = df.copy()
851871
expected.index.name = "index"
852872
check_round_trip(df, fp, expected=expected)
873+
874+
def test_timezone_aware_index(self, fp, timezone_aware_date_list):
875+
idx = 5 * [timezone_aware_date_list]
876+
877+
df = pd.DataFrame(index=idx, data={"col": 5 * [0]})
878+
879+
expected = df.copy()
880+
expected.index.name = "index"
881+
check_round_trip(df, fp, expected=expected)

0 commit comments

Comments
 (0)