Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions python/pyarrow/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
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