You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2018/01/07 17:28:23 UTC

[arrow] branch master updated: ARROW-1958: [Python] Error in pandas conversion for datetimetz row index

This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 234ff71  ARROW-1958: [Python] Error in pandas conversion for datetimetz row index
234ff71 is described below

commit 234ff713dd9ecbcc2824d336b4e4f9d059a490dd
Author: Albert Shieh <ad...@gmail.com>
AuthorDate: Sun Jan 7 12:28:19 2018 -0500

    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 <ad...@gmail.com>
    
    Closes #1454 from adshieh/master and squashes the following commits:
    
    6f413027 [Albert Shieh] Fix pandas conversion for datetimetz row index.
---
 python/pyarrow/pandas_compat.py             |  9 ++++++++-
 python/pyarrow/tests/test_convert_pandas.py | 10 ++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/python/pyarrow/pandas_compat.py b/python/pyarrow/pandas_compat.py
index f08049f..f3089d2 100644
--- a/python/pyarrow/pandas_compat.py
+++ b/python/pyarrow/pandas_compat.py
@@ -459,6 +459,8 @@ def _make_datetimetz(tz):
 
 def table_to_blockmanager(options, table, memory_pool, nthreads=1,
                           categoricals=None):
+    from pyarrow.compat import DatetimeTZDtype
+
     index_columns = []
     columns = []
     column_indexes = []
@@ -517,7 +519,12 @@ def table_to_blockmanager(options, table, memory_pool, nthreads=1,
                 # non-writeable arrays when calling MultiIndex.from_arrays
                 values = values.copy()
 
-            index_arrays.append(pd.Series(values, dtype=col_pandas.dtype))
+            if isinstance(col_pandas.dtype, DatetimeTZDtype):
+                index_array = (pd.Series(values).dt.tz_localize('utc')
+                               .dt.tz_convert(col_pandas.dtype.tz))
+            else:
+                index_array = pd.Series(values, dtype=col_pandas.dtype)
+            index_arrays.append(index_array)
             index_names.append(
                 _backwards_compatible_index_name(raw_name, logical_name)
             )
diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py
index 76b55cf..83b1da1 100644
--- a/python/pyarrow/tests/test_convert_pandas.py
+++ b/python/pyarrow/tests/test_convert_pandas.py
@@ -254,6 +254,16 @@ class TestPandasConversion(object):
         md = column_indexes['metadata']
         assert md['timezone'] == 'America/New_York'
 
+    def test_datetimetz_row_index(self):
+        df = pd.DataFrame({
+            'a': pd.date_range(
+                start='2017-01-01', periods=3, tz='America/New_York'
+            )
+        })
+        df = df.set_index('a')
+
+        _check_pandas_roundtrip(df, preserve_index=True)
+
     def test_categorical_row_index(self):
         df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
         df['a'] = df.a.astype('category')

-- 
To stop receiving notification emails like this one, please contact
['"commits@arrow.apache.org" <co...@arrow.apache.org>'].