Skip to content

Commit

Permalink
Backport PR #53078 on branch 2.0.x (BUG: pd.api.interchange.from_pand…
Browse files Browse the repository at this point in the history
…as raises with all-null categorical) (#53081)

Backport PR #53078: BUG: pd.api.interchange.from_pandas raises with all-null categorical

Co-authored-by: Marco Edward Gorelli <33491632+MarcoGorelli@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and MarcoGorelli committed May 4, 2023
1 parent 9135c3a commit 1bb9365
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
- Bug in :func:`api.interchange.from_dataframe` was raising ``IndexError`` on empty categorical data (:issue:`53077`)
- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`)
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/interchange/from_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ def categorical_column_to_series(col: Column) -> tuple[pd.Series, Any]:

# Doing module in order to not get ``IndexError`` for
# out-of-bounds sentinel values in `codes`
values = categories[codes % len(categories)]
if len(categories) > 0:
values = categories[codes % len(categories)]
else:
values = codes

cat = pd.Categorical(
values, categories=categories, ordered=categorical["is_ordered"]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def test_categorical_pyarrow():
tm.assert_frame_equal(result, expected)


def test_empty_categorical_pyarrow():
# https://github.com/pandas-dev/pandas/issues/53077
pa = pytest.importorskip("pyarrow", "11.0.0")

arr = [None]
table = pa.table({"arr": pa.array(arr, "float64").dictionary_encode()})
exchange_df = table.__dataframe__()
result = pd.api.interchange.from_dataframe(exchange_df)
expected = pd.DataFrame({"arr": pd.Categorical([np.nan])})
tm.assert_frame_equal(result, expected)


def test_large_string_pyarrow():
# GH 52795
pa = pytest.importorskip("pyarrow", "11.0.0")
Expand Down

0 comments on commit 1bb9365

Please sign in to comment.