You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "Matthew Rocklin (JIRA)" <ji...@apache.org> on 2018/12/31 23:02:00 UTC

[jira] [Commented] (ARROW-3652) [Python] CategoricalIndex is lost after reading back

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

Matthew Rocklin commented on ARROW-3652:
----------------------------------------

This is lost for categorical columns generally, not only the index


{code:python}
import pandas as pd
df = pd.DataFrame({'x': pd.Categorical(['a'])})
df.to_parquet('df.parquet')

import pyarrow.parquet as pq
table = pq.read_table('df.parquet', use_pandas_metadata=True)

>>> df.dtypes
x    category
dtype: object

>>> table.to_pandas().dtypes
x    object
dtype: object

>>> table
pyarrow.Table
x: string
__index_level_0__: int64
metadata
--------
{b'pandas': b'{"index_columns": ["__index_level_0__"], "column_indexes": [{"na'
            b'me": null, "field_name": null, "pandas_type": "unicode", "numpy_'
            b'type": "object", "metadata": {"encoding": "UTF-8"}}], "columns":'
            b' [{"name": "x", "field_name": "x", "pandas_type": "categorical",'
            b' "numpy_type": "int8", "metadata": {"num_categories": 1, "ordere'
            b'd": false}}, {"name": null, "field_name": "__index_level_0__", "'
            b'pandas_type": "int64", "numpy_type": "int64", "metadata": null}]'
            b', "pandas_version": "0.23.4"}'}
{code}


> [Python] CategoricalIndex is lost after reading back
> ----------------------------------------------------
>
>                 Key: ARROW-3652
>                 URL: https://issues.apache.org/jira/browse/ARROW-3652
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>    Affects Versions: 0.11.1
>            Reporter: Armin Berres
>            Priority: Major
>              Labels: parquet
>
> When a {{CategoricalIndex}} is written and read back the resulting index is not more categorical.
> {code}
> df = pd.DataFrame([['a', 'b'], ['c', 'd']], columns=['c1', 'c2'])
> df['c1'] = df['c1'].astype('category')
> df = df.set_index(['c1'])
> table = pa.Table.from_pandas(df)
> pq.write_table(table, 'test.parquet')
> ref_df = pq.read_pandas('test.parquet').to_pandas()
> print(df.index)
> # CategoricalIndex(['a', 'c'], categories=['a', 'c'], ordered=False, name='c1', dtype='category')
> print(ref_df.index)
> # Index(['a', 'c'], dtype='object', name='c1')
> {code}
> In the metadata the information is correctly contained:
> {code:java}
> {"name": "c1", "field_name": "c1", "p'
>             b'andas_type": "categorical", "numpy_type": "int8", "metadata": {"'
>             b'num_categories": 2, "ordered": false}
> {code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)