BUG: interchange categorical_column_to_series() should not accept only PandasColumn · Issue #49889 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pyarrow as pa import pandas as pd
arr = ["Mon", "Tue", "Mon", "Wed", "Mon", "Thu", "Fri", "Sat", "Sun"] table = pa.table( {"weekday": pa.array(arr).dictionary_encode()} ) exchange_df = table.dataframe()
from pandas.core.interchange.from_dataframe import from_dataframe from_dataframe(exchange_df)
Traceback (most recent call last): File "", line 1, in File "/Users/alenkafrim/repos/pyarrow-dev-9/lib/python3.9/site-packages/pandas/core/interchange/from_dataframe.py", line 53, in from_dataframe return _from_dataframe(df.dataframe(allow_copy=allow_copy)) File "/Users/alenkafrim/repos/pyarrow-dev-9/lib/python3.9/site-packages/pandas/core/interchange/from_dataframe.py", line 74, in _from_dataframe pandas_df = protocol_df_chunk_to_pandas(chunk) File "/Users/alenkafrim/repos/pyarrow-dev-9/lib/python3.9/site-packages/pandas/core/interchange/from_dataframe.py", line 124, in protocol_df_chunk_to_pandas columns[name], buf = categorical_column_to_series(col) File "/Users/alenkafrim/repos/pyarrow-dev-9/lib/python3.9/site-packages/pandas/core/interchange/from_dataframe.py", line 185, in categorical_column_to_series assert isinstance(cat_column, PandasColumn), "categories must be a PandasColumn" AssertionError: categories must be a PandasColumn
Issue Description
I am currently working on implementing a dataframe interchange protocol for pyarrow.Table
in Apache Arrow project (apache/arrow#14613).
I am using pandas implementation to test that the produced __dataframe__
object can be correctly consumed.
When consuming a pyarrow.Table
with categorical column I get an error from pandas that the categories must be a PandasColumn
and not a general __dataframe__
column defined by the interchange protocol. There is a check on line 185 for PandasColumn
instance:
def categorical_column_to_series(col: Column) -> tuple[pd.Series, Any]: |
---|
""" |
Convert a column holding categorical data to a pandas Series. |
Parameters |
---------- |
col : Column |
Returns |
------- |
tuple |
Tuple of pd.Series holding the data and the memory owner object |
that keeps the memory alive. |
""" |
categorical = col.describe_categorical |
if not categorical["is_dictionary"]: |
raise NotImplementedError("Non-dictionary categoricals not supported yet") |
cat_column = categorical["categories"] |
# for mypy/pyright |
assert isinstance(cat_column, PandasColumn), "categories must be a PandasColumn" |
Expected Behavior
categorical_column_to_series()
function should accept a general dataframe_protocol column for the categories in the categorical column.
Installed Versions
INSTALLED VERSIONS
commit : 87cfe4e
python : 3.9.14.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:46 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T8101
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.5.0
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.3.1
Cython : 0.29.28
pytest : 7.1.3
hypothesis : 6.39.4
sphinx : 4.3.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : 2022.02.0
matplotlib : 3.6.2
numba : 0.56.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0.dev117+geeca8a4e3.d20221122
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : 2022.11.0
xlrd : None
xlwt : None
zstandard : None
tzdata : None