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

Enable pd.Timestamp objects to be picklable when cudf.pandas is active #14474

Merged
merged 6 commits into from
Nov 27, 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
31 changes: 30 additions & 1 deletion python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import copyreg
import pickle
import sys

import pandas as pd
Expand Down Expand Up @@ -1304,3 +1305,31 @@ def _df_query_method(self, *args, local_dict=None, global_dict=None, **kwargs):
_Unusable,
typ,
)

# timestamps and timedeltas are not proxied, but non-proxied
# pandas types are currently not picklable. Thus, we define
# custom reducer/unpicker functions for these types:
def _reduce_obj(obj):
from cudf.pandas.module_accelerator import disable_module_accelerator

with disable_module_accelerator():
# args can contain objects that are unpicklable
# when the module accelerator is disabled
# (freq is of a proxy type):
pickled_args = pickle.dumps(obj.__reduce__())

return _unpickle_obj, (pickled_args,)


def _unpickle_obj(pickled_args):
from cudf.pandas.module_accelerator import disable_module_accelerator

with disable_module_accelerator():
unpickler, args = pickle.loads(pickled_args)
obj = unpickler(*args)
return obj


copyreg.dispatch_table[pd.Timestamp] = _reduce_obj
# same reducer/unpickler can be used for Timedelta:
copyreg.dispatch_table[pd.Timedelta] = _reduce_obj
4 changes: 4 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@ def test_np_array_of_timestamps():
xpd.Index(["a", 2, 3]),
# Other types
xpd.tseries.offsets.BDay(5),
xpd.Timestamp("2001-01-01"),
xpd.Timestamp("2001-01-01", freq="D"),
xpd.Timedelta("1 days"),
xpd.Timedelta(1, "D"),
],
)
def test_pickle(obj):
Expand Down