Skip to content

Commit

Permalink
Consolidate timestamp/timedelta functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shwina committed Nov 27, 2023
1 parent 3e36192 commit d8b6b15
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,49 +1306,30 @@ def _df_query_method(self, *args, local_dict=None, global_dict=None, **kwargs):
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):
# 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(ts.__reduce__())
pickled_args = pickle.dumps(obj.__reduce__())

return _unpickle_timestamp, (pickled_args,)
return _unpickle_obj, (pickled_args,)


def _unpickle_timedelta(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)
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,)
obj = unpickler(*args)
return obj


copyreg.dispatch_table[pd.Timestamp] = _reduce_timestamp
copyreg.dispatch_table[pd.Timedelta] = _reduce_timedelta
copyreg.dispatch_table[pd.Timestamp] = _reduce_obj
# same reducer/unpickler can be used for Timedelta:
copyreg.dispatch_table[pd.Timedelta] = _reduce_obj

0 comments on commit d8b6b15

Please sign in to comment.