-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adding arbitrary object serialization #1421
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import re | ||
import warnings | ||
from collections import Mapping, MutableMapping, Iterable | ||
from six.moves import cPickle as pickle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xarray doesn't depend on
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow sorry, I actually forgot it wasn't a builtin, and I lazily didn't set up a proper dev environment. Definitely a simple fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this try/except statement should probably go in xarray's pycompat module. |
||
|
||
import numpy as np | ||
import pandas as pd | ||
|
@@ -489,3 +490,13 @@ def ensure_us_time_resolution(val): | |
elif np.issubdtype(val.dtype, np.timedelta64): | ||
val = val.astype('timedelta64[us]') | ||
return val | ||
|
||
|
||
@functools.partial(np.vectorize, otypes='O') | ||
def encode_pickle(obj): | ||
return np.frombuffer(pickle.dumps(obj), dtype=np.uint8) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to use a later version of the pickle format -- at least version 2 (which introduced the binary version) if not For reference, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think six handles the protocol issue, which is why I didn't do anything here, but no six so we can handle that manually. I don't know much about pickle 2 and 3 compatability (i.e. dump in 2, load in 3), perhaps that would be the nicest configuration to default to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way that pickle works, any version of Python can load older pickles, but old versions of Python can't load newer pickles. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's set |
||
|
||
|
||
@functools.partial(np.vectorize, otypes='O') | ||
def decode_pickle(obj): | ||
return pickle.loads(obj.tostring()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
_infer_dtype
fails, we want to break out of this function and return the originalvar
, not copy the data and put it on a new Variable object (which happens below).