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

Add custom name setter and getter for proxy objects in cudf.pandas #16234

Merged
merged 4 commits into from
Jul 11, 2024
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
50 changes: 47 additions & 3 deletions python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ def Index__new__(cls, *args, **kwargs):
return self


def name(self):
return self._fsproxy_wrapped._name


def Index__setattr__(self, name, value):
if name.startswith("_"):
object.__setattr__(self, name, value)
return
if name == "name":
setattr(self._fsproxy_wrapped, "_name", value)
if name == "names":
setattr(self._fsproxy_wrapped, "_names", value)
return _FastSlowAttribute("__setattr__").__get__(self, type(self))(
name, value
)


Index = make_final_proxy_type(
"Index",
cudf.Index,
Expand All @@ -277,11 +294,13 @@ def Index__new__(cls, *args, **kwargs):
"__iter__": custom_iter,
"__init__": _DELETE,
"__new__": Index__new__,
"__setattr__": Index__setattr__,
"_constructor": _FastSlowAttribute("_constructor"),
"__array_ufunc__": _FastSlowAttribute("__array_ufunc__"),
"_accessors": set(),
"_data": _FastSlowAttribute("_data", private=True),
"_mask": _FastSlowAttribute("_mask", private=True),
"name": property(name),
},
)

Expand All @@ -292,7 +311,11 @@ def Index__new__(cls, *args, **kwargs):
fast_to_slow=lambda fast: fast.to_pandas(),
slow_to_fast=cudf.from_pandas,
bases=(Index,),
additional_attributes={"__init__": _DELETE},
additional_attributes={
"__init__": _DELETE,
"__setattr__": Index__setattr__,
"name": property(name),
},
)

SparseDtype = make_final_proxy_type(
Expand All @@ -319,7 +342,11 @@ def Index__new__(cls, *args, **kwargs):
fast_to_slow=lambda fast: fast.to_pandas(),
slow_to_fast=cudf.from_pandas,
bases=(Index,),
additional_attributes={"__init__": _DELETE},
additional_attributes={
"__init__": _DELETE,
"__setattr__": Index__setattr__,
"name": property(name),
},
)

Categorical = make_final_proxy_type(
Expand Down Expand Up @@ -350,6 +377,8 @@ def Index__new__(cls, *args, **kwargs):
"__init__": _DELETE,
"_data": _FastSlowAttribute("_data", private=True),
"_mask": _FastSlowAttribute("_mask", private=True),
"__setattr__": Index__setattr__,
"name": property(name),
},
)

Expand Down Expand Up @@ -385,6 +414,8 @@ def Index__new__(cls, *args, **kwargs):
"__init__": _DELETE,
"_data": _FastSlowAttribute("_data", private=True),
"_mask": _FastSlowAttribute("_mask", private=True),
"__setattr__": Index__setattr__,
"name": property(name),
},
)

Expand Down Expand Up @@ -441,6 +472,8 @@ def Index__new__(cls, *args, **kwargs):
"__init__": _DELETE,
"_data": _FastSlowAttribute("_data", private=True),
"_mask": _FastSlowAttribute("_mask", private=True),
"__setattr__": Index__setattr__,
"name": property(name),
},
)

Expand Down Expand Up @@ -474,14 +507,23 @@ def Index__new__(cls, *args, **kwargs):
additional_attributes={"__hash__": _FastSlowAttribute("__hash__")},
)


def names(self):
return self._fsproxy_wrapped._names


MultiIndex = make_final_proxy_type(
"MultiIndex",
cudf.MultiIndex,
pd.MultiIndex,
fast_to_slow=lambda fast: fast.to_pandas(),
slow_to_fast=cudf.from_pandas,
bases=(Index,),
additional_attributes={"__init__": _DELETE},
additional_attributes={
"__init__": _DELETE,
"__setattr__": Index__setattr__,
"name": property(names),
},
)

TimeGrouper = make_intermediate_proxy_type(
Expand Down Expand Up @@ -669,6 +711,8 @@ def Index__new__(cls, *args, **kwargs):
"__init__": _DELETE,
"_data": _FastSlowAttribute("_data", private=True),
"_mask": _FastSlowAttribute("_mask", private=True),
"__setattr__": Index__setattr__,
"name": property(name),
},
)

Expand Down
40 changes: 40 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,43 @@ def test_at_setitem_empty():
df.at[0, "new"] = 2.0
expected = pd.DataFrame({"name": [1.0], "new": [2.0]})
tm.assert_frame_equal(df, expected)


@pytest.mark.parametrize(
"index",
[
xpd.Index([1, 2, 3], name="foo"),
xpd.Index(["a", "b", "c"], name="foo"),
xpd.RangeIndex(start=0, stop=3, step=1, name="foo"),
xpd.CategoricalIndex(["a", "b", "a"], name="foo"),
xpd.DatetimeIndex(
["2024-04-24", "2025-04-24", "2026-04-24"], name="foo"
),
xpd.TimedeltaIndex(["1 days", "2 days", "3 days"], name="foo"),
xpd.PeriodIndex(
["2024-06", "2023-06", "2022-06"], freq="M", name="foo"
),
xpd.IntervalIndex.from_breaks([0, 1, 2, 3], name="foo"),
xpd.MultiIndex.from_tuples(
[(1, "a"), (2, "b"), (3, "c")], names=["foo1", "bar1"]
),
],
)
def test_change_index_name(index):
s = xpd.Series([1, 2, object()], index=index)
df = xpd.DataFrame({"values": [1, 2, object()]}, index=index)

if isinstance(index, xpd.MultiIndex):
names = ["foo2", "bar2"]
s.index.names = names
df.index.names = names

assert s.index.names == names
assert df.index.names == names
else:
name = "bar"
s.index.name = name
df.index.name = name

assert s.index.name == name
assert df.index.name == name
Loading