Skip to content

Commit 234ff71

Browse files
adshiehwesm
authored andcommitted
ARROW-1958: [Python] Error in pandas conversion for datetimetz row index
Fix conversion of datetimetz row index for non-UTC time zones in to_pandas. Author: Albert Shieh <[email protected]> Closes #1454 from adshieh/master and squashes the following commits: 6f41302 [Albert Shieh] Fix pandas conversion for datetimetz row index.
1 parent bda85bb commit 234ff71

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

python/pyarrow/pandas_compat.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ def _make_datetimetz(tz):
459459

460460
def table_to_blockmanager(options, table, memory_pool, nthreads=1,
461461
categoricals=None):
462+
from pyarrow.compat import DatetimeTZDtype
463+
462464
index_columns = []
463465
columns = []
464466
column_indexes = []
@@ -517,7 +519,12 @@ def table_to_blockmanager(options, table, memory_pool, nthreads=1,
517519
# non-writeable arrays when calling MultiIndex.from_arrays
518520
values = values.copy()
519521

520-
index_arrays.append(pd.Series(values, dtype=col_pandas.dtype))
522+
if isinstance(col_pandas.dtype, DatetimeTZDtype):
523+
index_array = (pd.Series(values).dt.tz_localize('utc')
524+
.dt.tz_convert(col_pandas.dtype.tz))
525+
else:
526+
index_array = pd.Series(values, dtype=col_pandas.dtype)
527+
index_arrays.append(index_array)
521528
index_names.append(
522529
_backwards_compatible_index_name(raw_name, logical_name)
523530
)

python/pyarrow/tests/test_convert_pandas.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ def test_datetimetz_column_index(self):
254254
md = column_indexes['metadata']
255255
assert md['timezone'] == 'America/New_York'
256256

257+
def test_datetimetz_row_index(self):
258+
df = pd.DataFrame({
259+
'a': pd.date_range(
260+
start='2017-01-01', periods=3, tz='America/New_York'
261+
)
262+
})
263+
df = df.set_index('a')
264+
265+
_check_pandas_roundtrip(df, preserve_index=True)
266+
257267
def test_categorical_row_index(self):
258268
df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
259269
df['a'] = df.a.astype('category')

0 commit comments

Comments
 (0)