Skip to content

Commit 59e6c80

Browse files
Backport PR pandas-dev#57175 on branch 2.2.x (BUG: Interchange protocol implementation handles empty dataframes incorrectly) (pandas-dev#57179)
Backport PR pandas-dev#57175: BUG: Interchange protocol implementation handles empty dataframes incorrectly Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent f6fd475 commit 59e6c80

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929

3030
Bug fixes
3131
~~~~~~~~~
32+
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
3233
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
3334

3435
.. ---------------------------------------------------------------------------

pandas/core/interchange/buffer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, x: np.ndarray, allow_copy: bool = True) -> None:
2323
"""
2424
Handle only regular columns (= numpy arrays) for now.
2525
"""
26-
if not x.strides == (x.dtype.itemsize,):
26+
if x.strides[0] and not x.strides == (x.dtype.itemsize,):
2727
# The protocol does not support strided buffers, so a copy is
2828
# necessary. If that's not allowed, we need to raise an exception.
2929
if allow_copy:

pandas/tests/interchange/test_impl.py

+9
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,12 @@ def test_large_string():
379379
result = pd.api.interchange.from_dataframe(df.__dataframe__())
380380
expected = pd.DataFrame({"a": ["x"]}, dtype="object")
381381
tm.assert_frame_equal(result, expected)
382+
383+
384+
def test_empty_dataframe():
385+
# https://github.com/pandas-dev/pandas/issues/56700
386+
df = pd.DataFrame({"a": []}, dtype="int8")
387+
dfi = df.__dataframe__()
388+
result = pd.api.interchange.from_dataframe(dfi, allow_copy=False)
389+
expected = pd.DataFrame({"a": []}, dtype="int8")
390+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)