Skip to content

Commit 26da007

Browse files
authored
fix(python): Fix interchange protocol data buffer dtype (#10787)
1 parent 149091a commit 26da007

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

py-polars/polars/interchange/column.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ def get_buffers(self) -> ColumnBuffers:
157157
def _get_data_buffer(self) -> tuple[PolarsBuffer, Dtype]:
158158
s = self._col._get_buffer(0)
159159
buffer = PolarsBuffer(s, allow_copy=self._allow_copy)
160-
161-
dtype = self.dtype
162-
if dtype[0] == DtypeKind.CATEGORICAL:
163-
dtype = (DtypeKind.UINT, 32, "I", Endianness.NATIVE)
164-
160+
dtype = polars_dtype_to_dtype(s.dtype)
165161
return buffer, dtype
166162

167163
def _get_validity_buffer(self) -> tuple[PolarsBuffer, Dtype] | None:

py-polars/tests/unit/interchange/test_column.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_get_buffers_with_validity_and_offsets() -> None:
206206
data_buffer, data_dtype = out["data"]
207207
expected = pl.Series([97, 98, 99, 195, 169, 195, 162, 195, 167], dtype=pl.UInt8)
208208
assert_series_equal(data_buffer._data, expected)
209-
assert data_dtype == (DtypeKind.STRING, 8, "U", "=")
209+
assert data_dtype == (DtypeKind.UINT, 8, "C", "=")
210210

211211
validity = out["validity"]
212212
assert validity is not None
@@ -260,14 +260,14 @@ def test_get_buffers_chunked_zero_copy_fails() -> None:
260260
(
261261
pl.Series(["a", "bc", None, "éâç"], dtype=pl.String),
262262
pl.Series([97, 98, 99, 195, 169, 195, 162, 195, 167], dtype=pl.UInt8),
263-
(DtypeKind.STRING, 8, "U", "="),
263+
(DtypeKind.UINT, 8, "C", "="),
264264
),
265265
(
266266
pl.Series(
267267
[datetime(1988, 1, 2), None, datetime(2022, 12, 3)], dtype=pl.Datetime
268268
),
269269
pl.Series([568080000000000, 0, 1670025600000000], dtype=pl.Int64),
270-
(DtypeKind.DATETIME, 64, "tsu:", "="),
270+
(DtypeKind.INT, 64, "l", "="),
271271
),
272272
(
273273
pl.Series(["a", "b", None, "a"], dtype=pl.Categorical),

py-polars/tests/unit/interchange/test_roundtrip.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def test_to_dataframe_pyarrow_zero_copy_parametric(df: pl.DataFrame) -> None:
5757
assert_frame_equal(result, df, categorical_as_str=True)
5858

5959

60+
@pytest.mark.skipif(
61+
sys.version_info < (3, 9),
62+
reason="The correct `from_dataframe` implementation for pandas is not available before Python 3.9",
63+
)
6064
@pytest.mark.filterwarnings(
6165
"ignore:.*PEP3118 format string that does not match its itemsize:RuntimeWarning"
6266
)
@@ -68,6 +72,10 @@ def test_to_dataframe_pandas_parametric(df: pl.DataFrame) -> None:
6872
assert_frame_equal(result, df, categorical_as_str=True)
6973

7074

75+
@pytest.mark.skipif(
76+
sys.version_info < (3, 9),
77+
reason="The correct `from_dataframe` implementation for pandas is not available before Python 3.9",
78+
)
7179
@pytest.mark.filterwarnings(
7280
"ignore:.*PEP3118 format string that does not match its itemsize:RuntimeWarning"
7381
)

0 commit comments

Comments
 (0)