Skip to content

Commit 5195db2

Browse files
committed
BUG: Pandas can't restore index from parquet with offset-specified timezone #35997
1 parent 1a576eb commit 5195db2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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[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[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

+18
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,12 @@ def test_timestamp_nanoseconds(self, pa):
712712
df = pd.DataFrame({"a": pd.date_range("2017-01-01", freq="1n", periods=10)})
713713
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
714714

715+
@td.skip_if_no("pyarrow", min_version="0.14")
716+
def test_timezone_aware_index(self, pa):
717+
idx = [datetime.datetime.now(datetime.timezone.utc)]
718+
df = pd.DataFrame(index=idx)
719+
check_round_trip(df, pa, write_kwargs={"version": "2.0"})
720+
715721
@td.skip_if_no("pyarrow", min_version="0.17")
716722
def test_filter_row_groups(self, pa):
717723
# https://github.com/pandas-dev/pandas/issues/26551
@@ -850,3 +856,15 @@ def test_empty_dataframe(self, fp):
850856
expected = df.copy()
851857
expected.index.name = "index"
852858
check_round_trip(df, fp, expected=expected)
859+
860+
def test_timezone_aware_index(self, fp):
861+
idx = [
862+
datetime.datetime.strptime(
863+
"2019-01-04T16:41:24+0200", "%Y-%m-%dT%H:%M:%S%z"
864+
)
865+
]
866+
df = pd.DataFrame(index=idx)
867+
868+
expected = df.copy()
869+
expected.index.name = "index"
870+
check_round_trip(df, fp, expected=expected)

0 commit comments

Comments
 (0)