[ 
https://issues.apache.org/jira/browse/ARROW-1743?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16250782#comment-16250782
 ] 

ASF GitHub Bot commented on ARROW-1743:
---------------------------------------

wesm closed pull request #1260: ARROW-1743: [Python] Avoid non-array 
writeable-flag check
URL: https://github.com/apache/arrow/pull/1260
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/pyarrow/pandas_compat.py b/python/pyarrow/pandas_compat.py
index 5592d8dd9..2ecdb63da 100644
--- a/python/pyarrow/pandas_compat.py
+++ b/python/pyarrow/pandas_compat.py
@@ -404,7 +404,7 @@ def table_to_blockmanager(options, table, memory_pool, 
nthreads=1):
             index_name = None if is_unnamed_index_level(name) else name
             col_pandas = col.to_pandas()
             values = col_pandas.values
-            if not values.flags.writeable:
+            if hasattr(values, 'flags') and not values.flags.writeable:
                 # ARROW-1054: in pandas 0.19.2, factorize will reject
                 # non-writeable arrays when calling MultiIndex.from_arrays
                 values = values.copy()
diff --git a/python/pyarrow/tests/test_convert_pandas.py 
b/python/pyarrow/tests/test_convert_pandas.py
index 8360dae54..5d8f63279 100644
--- a/python/pyarrow/tests/test_convert_pandas.py
+++ b/python/pyarrow/tests/test_convert_pandas.py
@@ -198,6 +198,13 @@ def test_datetimetz_column_index(self):
         md = column_indexes['metadata']
         assert md['timezone'] == 'America/New_York'
 
+    def test_categorical_row_index(self):
+        df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
+        df['a'] = df.a.astype('category')
+        df = df.set_index('a')
+
+        self._check_pandas_roundtrip(df, preserve_index=True)
+
     def test_float_no_nulls(self):
         data = {}
         fields = []


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> [Python] Table to_pandas fails when index contains categorical column
> ---------------------------------------------------------------------
>
>                 Key: ARROW-1743
>                 URL: https://issues.apache.org/jira/browse/ARROW-1743
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>    Affects Versions: 0.7.1
>            Reporter: Brian Pendleton
>            Assignee: Licht Takeuchi
>              Labels: pull-request-available
>             Fix For: 0.8.0
>
>
> Categorical columns in the index of a dataframe are causing a roundtrip 
> failure.  
> {code}
> >>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
> >>> df['a'] = df.a.astype('category')
> >>> df = df.set_index('a')
> >>> tbl = pa.Table.from_pandas(df)
> >>> tbl.to_pandas()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "table.pxi", line 881, in pyarrow.lib.Table.to_pandas
>   File 
> "C:\Users\bpendlet\Miniconda3\envs\panpy3\lib\site-packages\pyarrow\pandas_compat.py",
>  line 303, in table_to_blockmanager
>     if not values.flags.writeable:
> AttributeError: 'Categorical' object has no attribute 'flags'
> {code}
> Works as expected when you don't change have the categorical:
> {code}
> >>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
> >>> df = df.set_index('a')
> >>> tbl = pa.Table.from_pandas(df)
> >>> tbl.to_pandas()
>    b
> a
> 1  1
> 2  2
> 3  3
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to