-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Tslibs offsets immutable #18224
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
Tslibs offsets immutable #18224
Conversation
@@ -361,6 +370,58 @@ class _BaseOffset(object): | |||
out = '<%s' % n_str + className + plural + self._repr_attrs() + '>' | |||
return out | |||
|
|||
def __setstate__(self, state): | |||
"""Reconstruct an instance from a pickled state""" |
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.
no just define dunder reduce
you can’t use getstate/setstate in cython class
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.
I'll try again with __reduce__
. Don't hold your breath.
If we define __setstate__
in the non-cython class, trying to set self.n raises an AttributeError because it is readonly.
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.
Isn't __reduce__
for pickling? The problem here is un-pickling legacy offsets.
this is not a feasible work around. you will have to look how I handled the ndarray subclassing case (this is similar). |
OK, I'll keep poking at it. Where did the ndarray subclassing occur? Best guess is IndexOpsMixin? |
Will wrk be m this locally, get it off reviewers plate for now. Closing. |
I'm not having much luck with this, have not given up. One idea: could we drop support for the 0.15.2 pickle? That would solve the current problem. |
Implementing |
@jreback suggestions on who I might be able to ask for help on this? Making offsets immutable is a pretty high priority for me (major PeriodIndex perf implications) and this is dead in the water. |
@jbrockmendel not sure what is the specific issue |
TLDR: cython class + pickle + back-compat We need to make |
I actually don’t have a problem with breaking pickles for offsets; you wooild rarely pickle these anyways but i think u can still do this by using our substitution machinery - why don’t i give this a try |
Awesome, go for it! My attempt to use the substitution machinery didn't work out, so with a little luck I'll learn something from seeing the results.
If the substitution approach doesn't work out, then after my remaining outstanding PRs are closed out I'll do it this way. |
@jreback gotten a chance to look at this? I've been trying periodically and still haven't come up with anything other than the hack in |
@jbrockmendel I couldn't pull down your branch locally (deleted?) but maybe this article helps: Namely that "You can't use object as the terminating class for your inheritance if you want to pass arguments to |
Yah, looks like I deleted the branch. I'll see if I can restore it without creating a new PR.
Nope, cython raises a compile-time error telling you to use |
@WillAyd the branch should be ~restored (made a new branch with the same name, copy/pasted the diff). |
Still trying to recreate from the branch provided and haven't seen through yet but wondering at the moment if even just making a pass-through def __cinit__(self, *args):
pass My current thinking there is a result of the below link: Namely "If you anticipate subclassing your extension type in Python, you may find it useful to give the cinit() method * and ** arguments so that it can accept and ignore extra arguments. Otherwise, any Python subclass which has an init() with a different signature will have to override new() [1] as well as init(), which the writer of a Python class wouldn’t expect to have to do." Doing such did resolve the TypeError you mentioned originally, though some other errors popped up I haven't had time to look at and may or may not have been resolved by subsequent work you did. Sharing for now in case this finding helps you at all |
Thanks for taking a look. Note that when I put this together yesterday I got a test error in something like |
I don't get any errors when reverting the changes made in |
Not even in |
No errors at all running pytest pandas/tests/io/test_pickle.py This was using python3 so maybe that's the differentiator |
Hmm I've fixed the v0_15_2 error I was getting (had mixed and matched diffs), but I'm still getting failures when I revert the pickle_compat edits (py27 and py36, cython 0.28.3). |
This is the big hurdle towards making DateOffsets immutable.
n
andnormalize
to be readonlyI'm not wild about the hack in pickle_compat, but haven't found any other way to avoid
TestCommon.test_pickle_v0_15_2
failing with:After this there will then be a) a bunch of other attributes to make readonly and b) the optimizations that are available once DateOffsets are immutable. But first we need to either OK the pickle_compat hack or find another way around.