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

Return same type as the original index for .loc operations #15717

Merged
merged 5 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ def _getitem_tuple_arg(self, arg):
# as join is not assigning any names to index,
# update it over here
df.index.name = columns_df.index.name
if not columns_df._data.multiindex and is_numeric_dtype(
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
df.index.dtype
):
# Preserve the original index type.
df.index = df.index.astype(self._frame.index.dtype)
df = df.sort_values(by=[tmp_col_name, cantor_name])
df.drop(columns=[tmp_col_name, cantor_name], inplace=True)
# There were no indices found
Expand Down
18 changes: 18 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11010,3 +11010,21 @@ def test_dataframe_init_with_nans():
assert gdf["a"].dtype == np.dtype("float64")
pdf = pd.DataFrame({"a": [1, 2, 3, np.nan]})
assert_eq(pdf, gdf)


@pytest.mark.parametrize("dtype1", ["int16", "float32"])
@pytest.mark.parametrize("dtype2", ["int16", "float32"])
def test_dataframe_loc_int_float(dtype1, dtype2):
df = cudf.DataFrame(
{"a": [10, 11, 12, 13, 14]},
index=cudf.Index([1, 2, 3, 4, 5], dtype=dtype1),
)
pdf = df.to_pandas()

gidx = cudf.Index([2, 3, 4], dtype=dtype2)
pidx = gidx.to_pandas()

actual = df.loc[gidx]
expected = pdf.loc[pidx]

assert_eq(actual, expected, check_index_type=True, check_dtype=True)
Loading