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 5 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: 49 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,50 @@ def _df_query_method(self, *args, local_dict=None, global_dict=None, **kwargs):
_Unusable,
typ,
)

# timestamps and timedeltas are not proxied and "real" pandas types
# are currently not picklable when the module accelerator is enabled.
def _unpickle_timestamp(pickled_args):
from cudf.pandas.module_accelerator import disable_module_accelerator

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


def _reduce_timestamp(ts):
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(ts.__reduce__())

return _unpickle_timestamp, (pickled_args,)


def _unpickle_timedelta(pickled_args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is _unpickle_timedelta different from _unpickle_timestamp? Similarly for _reduce_*. They look identical to me. Do we need this duplication or can we simplify things?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this looks like it can be deduplicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you are -- they are the same function and I've deduplicated

from cudf.pandas.module_accelerator import disable_module_accelerator

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


def _reduce_timedelta(ts):
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(ts.__reduce__())

return _unpickle_timedelta, (pickled_args,)


copyreg.dispatch_table[pd.Timestamp] = _reduce_timestamp
copyreg.dispatch_table[pd.Timedelta] = _reduce_timedelta
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
Loading