Skip to content

Commit bc5b9d9

Browse files
mroeschketopper-123
authored andcommitted
BUG: from_dataframe with empty objects (pandas-dev#53161)
* BUG: from_dataframe with empty objects * Use protocol_df_chunk_to_pandas
1 parent f83fdcf commit bc5b9d9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ Metadata
488488
Other
489489
^^^^^
490490
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
491+
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
491492
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
492493
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
493494
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)

pandas/core/interchange/from_dataframe.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True):
7979
raise RuntimeError(
8080
"To join chunks a copy is required which is forbidden by allow_copy=False"
8181
)
82-
if len(pandas_dfs) == 1:
82+
if not pandas_dfs:
83+
pandas_df = protocol_df_chunk_to_pandas(df)
84+
elif len(pandas_dfs) == 1:
8385
pandas_df = pandas_dfs[0]
8486
else:
8587
pandas_df = pd.concat(pandas_dfs, axis=0, ignore_index=True, copy=False)

pandas/tests/interchange/test_impl.py

+12
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,15 @@ def test_categorical_to_numpy_dlpack():
272272
result = np.from_dlpack(col.get_buffers()["data"][0])
273273
expected = np.array([0, 1, 0], dtype="int8")
274274
tm.assert_numpy_array_equal(result, expected)
275+
276+
277+
@pytest.mark.parametrize("data", [{}, {"a": []}])
278+
def test_empty_pyarrow(data):
279+
# GH 53155
280+
pytest.importorskip("pyarrow", "11.0.0")
281+
from pyarrow.interchange import from_dataframe as pa_from_dataframe
282+
283+
expected = pd.DataFrame(data)
284+
arrow_df = pa_from_dataframe(expected)
285+
result = from_dataframe(arrow_df)
286+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)