Skip to content

Commit 6901e6d

Browse files
add validation
1 parent b63e601 commit 6901e6d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pandas/core/frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,17 @@ def from_arrow(cls, data):
17761776
"""
17771777
pa = import_optional_dependency("pyarrow", min_version="14.0.0")
17781778
if not isinstance(data, pa.Table):
1779+
if not (
1780+
hasattr(data, "__arrow_c_array__")
1781+
or hasattr(data, "__arrow_c_stream__")
1782+
):
1783+
# explicitly test this, because otherwise we would accept variour other
1784+
# input types through the pa.table(..) call
1785+
raise TypeError(
1786+
"Expected an Arrow-compatible tabular object (i.e. having an "
1787+
"'_arrow_c_array__' or '__arrow_c_stream__' method), got "
1788+
f"'{type(data).__name__}' instead."
1789+
)
17791790
data = pa.table(data)
17801791

17811792
df = data.to_pandas()

pandas/tests/frame/test_arrow_interface.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ def test_dataframe_from_arrow():
8787

8888
result = pd.DataFrame.from_arrow(ArrowArrayWrapper(batch))
8989
tm.assert_frame_equal(result, expected)
90+
91+
# only accept actual Arrow objects
92+
with pytest.raises(TypeError, match="Expected an Arrow-compatible tabular object"):
93+
pd.DataFrame.from_arrow({"a": [1, 2, 3], "b": ["a", "b", "c"]})

0 commit comments

Comments
 (0)