Skip to content

Commit dc16177

Browse files
authored
BUG: Interchange protocol implementation handles empty dataframes incorrectly (#57175)
fix from_dataframe for empty dataframes
1 parent 27b6996 commit dc16177

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
@@ -392,3 +392,12 @@ def test_large_string():
392392
result = pd.api.interchange.from_dataframe(df.__dataframe__())
393393
expected = pd.DataFrame({"a": ["x"]}, dtype="object")
394394
tm.assert_frame_equal(result, expected)
395+
396+
397+
def test_empty_dataframe():
398+
# https://github.com/pandas-dev/pandas/issues/56700
399+
df = pd.DataFrame({"a": []}, dtype="int8")
400+
dfi = df.__dataframe__()
401+
result = pd.api.interchange.from_dataframe(dfi, allow_copy=False)
402+
expected = pd.DataFrame({"a": []}, dtype="int8")
403+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)