Skip to content

Commit

Permalink
Add BaseOffset as a final proxy type to pass instancechecks for offse…
Browse files Browse the repository at this point in the history
…ts against `BaseOffset` (#14678)

Addresses part of #14674.

This PR makes `cudf.pandas` aware of the `BaseOffset` type. Because of the way `isinstance()` checks work on proxy types*, we will now pass instance checks like:

```python
freq = "D"
assert isinstance(pd.tseries.frequencies.to_offset(freq), pd.tseries.offsets.BaseOffset)
```

*proxy types appear to be related to each other in the same way as their corresponding slow types. So if `A` inherits from `B`, then `ProxyA` _appears_ to inherit from `ProxyB` even though they are not _actually_ related by inheritance.

Authors:
  - Ashwin Srinath (https://github.com/shwina)
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #14678
  • Loading branch information
shwina authored Jan 19, 2024
1 parent a38fc01 commit bfbaf3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import copyreg
Expand Down Expand Up @@ -1036,6 +1036,15 @@ def _df_query_method(self, *args, local_dict=None, global_dict=None, **kwargs):
additional_attributes={"__hash__": _FastSlowAttribute("__hash__")},
)

BaseOffset = make_final_proxy_type(
"BaseOffset",
_Unusable,
pd.offsets.BaseOffset,
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={"__hash__": _FastSlowAttribute("__hash__")},
)

Day = make_final_proxy_type(
"Day",
_Unusable,
Expand Down
5 changes: 5 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,8 @@ def test_concat_fast():
def test_func_namespace():
# note: this test is sensitive to Pandas' internal module layout
assert xpd.concat is xpd.core.reshape.concat.concat


def test_isinstance_base_offset():
offset = xpd.tseries.frequencies.to_offset("1s")
assert isinstance(offset, xpd.tseries.offsets.BaseOffset)

0 comments on commit bfbaf3f

Please sign in to comment.