Skip to content

Commit

Permalink
ARROW-6652: [Python] Fix ChunkedArray.to_pandas to retain timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Sep 24, 2019
1 parent 35b6d07 commit 5122451
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,16 @@ 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):
tz = self.type.tz
if tz is not None:
tz = string_to_tzinfo(tz)
result = (result.dt.tz_localize('utc')
.dt.tz_convert(tz))

return result

def __array__(self, dtype=None):
values = self.to_pandas().values
Expand Down
3 changes: 3 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions python/pyarrow/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5122451

Please sign in to comment.