Skip to content

Commit c410146

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

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

pandas/_libs/tslibs/timezones.pyx

+19-2
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

@@ -103,7 +103,24 @@ cpdef inline tzinfo maybe_get_tz(object tz):
103103
if isinstance(tz, _dateutil_tzfile) and '.tar.gz' in tz._filename:
104104
tz._filename = zone
105105
else:
106-
tz = pytz.timezone(tz)
106+
if tz[0] == '-':
107+
hours = int(tz[1:3])
108+
minutes = int(tz[4:6])
109+
tz = timezone(-timedelta(hours=hours, minutes=minutes))
110+
elif tz[0] == '+':
111+
hours = int(tz[1:3])
112+
minutes = int(tz[4:6])
113+
tz = timezone(timedelta(hours=hours, minutes=minutes))
114+
elif tz[0:4] == 'UTC-':
115+
hours = int(tz[4:6])
116+
minutes = int(tz[7:9])
117+
tz = timezone(-timedelta(hours=hours, minutes=minutes))
118+
elif tz[0:4] == 'UTC+':
119+
hours = int(tz[4:6])
120+
minutes = int(tz[7:9])
121+
tz = timezone(timedelta(hours=hours, minutes=minutes))
122+
else:
123+
tz = pytz.timezone(tz)
107124
elif is_integer_object(tz):
108125
tz = pytz.FixedOffset(tz / 60)
109126
elif isinstance(tz, tzinfo):

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)