Skip to content

Commit 3cbdd86

Browse files
jorisvandenbosschenoatamir
authored andcommitted
BUG: ensure to return writable buffer in __dataframe__ interchange for categorical column (pandas-dev#48419)
* BUG: ensure to return writable buffer in __dataframe__ interchange for categorical column * typo
1 parent 9841631 commit 3cbdd86

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/interchange/column.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _get_data_buffer(
270270
buffer = PandasBuffer(self._col.to_numpy(), allow_copy=self._allow_copy)
271271
dtype = self.dtype
272272
elif self.dtype[0] == DtypeKind.CATEGORICAL:
273-
codes = self._col.values.codes
273+
codes = self._col.values._codes
274274
buffer = PandasBuffer(codes, allow_copy=self._allow_copy)
275275
dtype = self._dtype_from_pandasdtype(codes.dtype)
276276
elif self.dtype[0] == DtypeKind.STRING:

pandas/tests/interchange/test_impl.py

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from pandas._libs.tslibs import iNaT
8+
import pandas.util._test_decorators as td
89

910
import pandas as pd
1011
import pandas._testing as tm
@@ -193,3 +194,13 @@ def test_datetime():
193194
assert col.describe_null == (ColumnNullType.USE_SENTINEL, iNaT)
194195

195196
tm.assert_frame_equal(df, from_dataframe(df.__dataframe__()))
197+
198+
199+
@td.skip_if_np_lt("1.23")
200+
def test_categorical_to_numpy_dlpack():
201+
# https://github.com/pandas-dev/pandas/issues/48393
202+
df = pd.DataFrame({"A": pd.Categorical(["a", "b", "a"])})
203+
col = df.__dataframe__().get_column_by_name("A")
204+
result = np.from_dlpack(col.get_buffers()["data"][0])
205+
expected = np.array([0, 1, 0], dtype="int8")
206+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)