diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index e47327ff2d4..e35717d7424 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -903,12 +903,10 @@ cdef class Array(_PandasConvertible): self, &out)) result = pandas_api.series(wrap_array_output(out), name=self._name) - if isinstance(self.type, TimestampType): - tz = self.type.tz - if tz is not None: - tz = string_to_tzinfo(tz) - result = (result.dt.tz_localize('utc') - .dt.tz_convert(tz)) + if isinstance(self.type, TimestampType) and self.type.tz is not None: + from pyarrow.pandas_compat import make_tz_aware + + result = make_tz_aware(result, self.type.tz) return result diff --git a/python/pyarrow/pandas_compat.py b/python/pyarrow/pandas_compat.py index 4b7e2e02987..900711ab6bd 100644 --- a/python/pyarrow/pandas_compat.py +++ b/python/pyarrow/pandas_compat.py @@ -1046,3 +1046,17 @@ def _add_any_metadata(table, pandas_metadata): return pa.Table.from_arrays(columns, schema=pa.schema(fields)) else: return table + + +# ---------------------------------------------------------------------- +# Helper functions used in lib + + +def make_tz_aware(series, tz): + """ + Make a datetime64 Series timezone-aware for the given tz + """ + tz = pa.lib.string_to_tzinfo(tz) + series = (series.dt.tz_localize('utc') + .dt.tz_convert(tz)) + return series diff --git a/python/pyarrow/table.pxi b/python/pyarrow/table.pxi index a09766fd4b7..086c4f45011 100644 --- a/python/pyarrow/table.pxi +++ b/python/pyarrow/table.pxi @@ -163,7 +163,14 @@ cdef class ChunkedArray(_PandasConvertible): self.sp_chunked_array, self, &out)) - return pandas_api.series(wrap_array_output(out), name=self._name) + result = pandas_api.series(wrap_array_output(out), name=self._name) + + if isinstance(self.type, TimestampType) and self.type.tz is not None: + from pyarrow.pandas_compat import make_tz_aware + + result = make_tz_aware(result, self.type.tz) + + return result def __array__(self, dtype=None): values = self.to_pandas().values diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 9aa8a7c8b48..91ca247aa27 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -1786,3 +1786,6 @@ def test_to_pandas_timezone(): arr = pa.array([1, 2, 3], type=pa.timestamp('s', tz='Europe/Brussels')) s = arr.to_pandas() assert s.dt.tz is not None + arr = pa.chunked_array([arr]) + s = arr.to_pandas() + assert s.dt.tz is not None diff --git a/python/pyarrow/tests/test_table.py b/python/pyarrow/tests/test_table.py index f9b2092ce07..434ec8029c1 100644 --- a/python/pyarrow/tests/test_table.py +++ b/python/pyarrow/tests/test_table.py @@ -187,15 +187,19 @@ def test_chunked_array_pickle(data, typ): @pytest.mark.pandas def test_chunked_array_to_pandas(): + import pandas as pd + data = [ pa.array([-10, -5, 0, 5, 10]) ] table = pa.table(data, names=['a']) col = table.column(0) assert isinstance(col, pa.ChunkedArray) - array = col.to_pandas() - assert array.shape == (5,) - assert array[0] == -10 + series = col.to_pandas() + assert isinstance(series, pd.Series) + assert series.shape == (5,) + assert series[0] == -10 + assert series.name == 'a' @pytest.mark.pandas