-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pickle incompatibility with pandas-2.0 from pandas-1.5 with numeric index #53300
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
Comments
Possibly a hack, but a workaround is to re-create from pandas.core.indexes.base import Index
class NumericIndex(Index):
pass
class IntegerIndex(NumericIndex):
pass
class Int64Index(IntegerIndex):
pass
class UInt64Index(IntegerIndex):
pass
class Float64Index(NumericIndex):
pass resolves the issue, and |
We support pickling through the
? |
Is pickle even supposed to support cross-major.minor compatibility? |
Looking at the binary structure from But, this reminds me that print(pd.__version__) # 2.0.1
print(pd.read_pickle("/tmp/s-1.5.3.pkl"))
# 2 3.3
# 4 4.4
# dtype: float64 the doc also neatly answers the compatibility question:
So yes, I'd expect |
From the code above, it appears to work. If so, can you close this? |
Re-opening, because the issue actually manifested from a custom class with DataFrame attributes, which cannot use Here is a revised example, first define a custom class with class MyClass:
def __init__(self, thing, df):
self.thing = thing
self.df = df
def __iter__(self):
yield "thing", self.thing
yield "df", self.df then using pandas-1.5.3: import pandas as pd
import pickle
from myclass import MyClass
mc = MyClass(1.23, pd.DataFrame({"s": [3.3, 4.4]}, index=[2, 4]))
with open("mc-1.5.3.pkl", "wb") as f:
pickle.dump(mc, f)
with open("mc-1.5.3.pkl", "rb") as f:
mc2 = pickle.load(f) using The only solution that I see is to use the private API, here with pandas-2.0.1: import pandas as pd
from myclass import MyClass
with open("mc-1.5.3.pkl", "rb") as f:
mc2 = pd.compat.pickle_compat.load(f)
dict(mc2)
# {'thing': 1.23,
# 'df': s
# 2 3.3
# 4 4.4} Is there any other way to unpickle a custom class using the public API? |
whoops, my bad! It seems the public API works fine with this one too: import pandas as pd
from myclass import MyClass
mc2 = pd.read_pickle("mc-1.5.3.pkl")
dict(mc2)
# {'thing': 1.23,
# 'df': s
# 2 3.3
# 4 4.4} the |
Ok, great. We only support reading/writing pickles through the |
Hi, as this is pretty new topic and can be a crucial point for guys who might have significant amount of data stored in a form of pickle → pandas, and to help google providing a match, I would like to highlight that only solution for my data lib migrating from pandas < 2 to pandas >= 2 was pd.compat.pickle_compat.load(X) Other discussions and stackoverflow offer only "pd.read_pickle(X)" which didn't work in my case. Best, |
I have written a substantial amount of files using Is this for pandas or joblib to solve? Or do I have to walk my directories, load all files with joblib and dump them back with pickle? I was using |
Hi, the same problem occurs with pickle files that have been created with These files are opened by:
The proposed
The alternative
The hack above, creating a file |
Going by pandas-dev/pandas#53300 this should be backwards compatible with older versions of pandas down to 0.20.3.
Going by pandas-dev/pandas#53300 this should be backwards compatible with older versions of pandas down to 0.20.3.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Create a simple series with a numeric index with pandas-1.5.3:
Or get it here: s-1.5.3.pkl.gz
then try to unpickle it with pandas-2.0:
raises:
Issue Description
Some pickle objects created with pandas<2 cannot be loaded with pandas-2.0, since there is no longer a
pandas.core.indexes.numeric
module (removed with #51139).See also Stack Overflow Q/A for this issue (not mine).
Expected Behavior
There is a
pandas.compat.pickle_compat
module that could potentially be used to help this scenario and read the pickle? Curiously enough, it referencespandas.core.indexes.numeric
which was removed for 2.0.Installed Versions
The text was updated successfully, but these errors were encountered: