If someone uses pandas.DataFrame.to_pickle() , do the following modification in the source code to be able to configure the pickle protocol:
1) In the source file /pandas/io/pickle.py (before changing, copy the source file as /pandas/io/pickle.py.ori ), find the following lines:
def to_pickle(obj, path): pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL)
Change these lines to:
def to_pickle(obj, path, protocol=pkl.HIGHEST_PROTOCOL): pkl.dump(obj, f, protocol=protocol)
2) In the source file /pandas/core/generic.py (before changing, copy the source file as /pandas/core/generic.py.ori ), find the following lines:
def to_pickle(self, path): return to_pickle(self, path)
Change these lines to:
def to_pickle(self, path, protocol=None): return to_pickle(self, path, protocol)
3) Restart the python kernel if it starts, then save your framework using the available pickle protocol (0, 1, 2, 3, 4):
# Python 2.x can read this df.to_pickle('my_dataframe.pck', protocol=2)
4) After updating pandas, repeat steps 1 and 2.
5) (optional) Ask the developers to have this feature in the official versions (because your code will throw exceptions in any other Python environment without these changes)
A good day!