Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Fix from_arrow to load a sliced arrow table #12665

Merged
merged 3 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5173,13 +5173,21 @@ def from_arrow(cls, table):

if index_col:
if isinstance(index_col[0], dict):
out = out.set_index(
cudf.RangeIndex(
index_col[0]["start"],
index_col[0]["stop"],
name=index_col[0]["name"],
)
idx = cudf.RangeIndex(
index_col[0]["start"],
index_col[0]["stop"],
name=index_col[0]["name"],
)
if len(idx) == len(out):
# `idx` is generated from arrow `pandas_metadata`
# which can get out of date with many of the
# arrow operations. Hence verifying if the
# lengths match, or else don't need to set
# an index at all i.e., Default RangeIndex
# will be set.
# See more about the discussion here:
# https://github.com/apache/arrow/issues/15178
out = out.set_index(idx)
else:
out = out.set_index(index_col[0])

Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10011,3 +10011,17 @@ def test_dataframe_transpose_complex_types(data):
actual = gdf.T

assert_eq(expected, actual)


def test_dataframe_from_arrow_slice():
table = pa.Table.from_pandas(
pd.DataFrame.from_dict(
{"a": ["aa", "bb", "cc"] * 3, "b": [1, 2, 3] * 3}
)
)
table_slice = table.slice(3, 7)

expected = table_slice.to_pandas()
actual = cudf.DataFrame.from_arrow(table_slice)

assert_eq(expected, actual)