Skip to content

Commit

Permalink
add helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Sep 24, 2019
1 parent 5122451 commit 89d0044
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
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
10 changes: 4 additions & 6 deletions python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ cdef class ChunkedArray(_PandasConvertible):

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

0 comments on commit 89d0044

Please sign in to comment.