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

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

Right, I've just done a search on this topic and found a few other issues.  
Happy to move conversation to one of them if desired.

In this particular case we already have pandas-specific metadata telling us 
that we have a categorical column (this is in the `pandas_type` entry for the 
column).  As you say we don't know the category-mapping, but we could still 
create a Categorical column and just let Pandas decide the mapping.  I suspect 
that it sorts the entries as a sane default..  This wouldn't necessarily 
roundtrip the right category-to-code mapping, but we can at least roundtrip the 
fact that these should be represented as categoricals.

Speaking from a Dask Dataframe perspective this would be desirable.  We're 
accustomed to having to remap mismatched categorical columns to make things 
match up.  Pandas generally handles this case well today.  However, I can 
understand if Arrow wants to play things safe here and resist the temptation to 
let Pandas be magical.

 

So, the concrete ask here is that when converting a table to a Pandas dataframe 
and using the `use_pandas_metadata=` keyword that we convert columns that 
Pandas marked with `pandas_type='category'` to be categorical columns.

> [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)

Reply via email to