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

Fix empty string column construction #14052

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,24 @@ def as_column(
from_pandas=True if nan_as_null is None else nan_as_null,
)

if (
isinstance(pyarrow_array, pa.NullArray)
and pa_type is None
and dtype is None
):
if getattr(arbitrary, "dtype", None) == cudf.dtype(
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
"object"
):
# pa.array constructor returns a NullArray
# for empty arrays, instead of a StringArray.
# This issue is only specific to this dtype,
# all other dtypes, result in their corresponding
# arrow array creation.
dtype = cudf.dtype("str")
pyarrow_array = pyarrow_array.cast(
np_to_pa_dtype(dtype)
)

if (
isinstance(arbitrary, pd.Index)
and arbitrary.dtype == cudf.dtype("object")
Expand Down
5 changes: 1 addition & 4 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7256,10 +7256,7 @@ def test_dataframe_keys(df):
def test_series_keys(ps):
gds = cudf.from_pandas(ps)

if len(ps) == 0 and not isinstance(ps.index, pd.RangeIndex):
assert_eq(ps.keys().astype("float64"), gds.keys())
else:
assert_eq(ps.keys(), gds.keys())
assert_eq(ps.keys(), gds.keys())


@pytest_unmark_spilling
Expand Down
24 changes: 24 additions & 0 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SIGNED_INTEGER_TYPES,
SIGNED_TYPES,
UNSIGNED_TYPES,
ALL_TYPES,
_create_pandas_series,
assert_column_memory_eq,
assert_column_memory_ne,
Expand Down Expand Up @@ -2698,3 +2699,26 @@ def test_index_getitem_time_duration(dtype):
assert gidx[i] is pidx[i]
else:
assert_eq(gidx[i], pidx[i])


@pytest.mark.parametrize("dtype", ALL_TYPES)
def test_index_empty_from_pandas(request, dtype):
request.node.add_marker(
pytest.mark.xfail(
condition=not PANDAS_GE_200
and dtype
in {
"datetime64[ms]",
"datetime64[s]",
"datetime64[us]",
"timedelta64[ms]",
"timedelta64[s]",
"timedelta64[us]",
},
reason="Fixed in pandas-2.0",
)
)
pidx = pd.Index([], dtype=dtype)
gidx = cudf.from_pandas(pidx)

assert_eq(pidx, gidx)