I'm serializing some data into a pickle file. Unfortunately the structure of the data might change. Therefore I have a static VERSION number in the code that is incremented if the data structure has changed. In such case the data from the pickle file is invalid and should be discarded.
Therefore I tried to save a tuple consisting of the data and a version number. But restoring it from pickle raises a UnicodeDecodeError:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)
I wonder how you would include a version number? Embedding it in the file path is an option, but much more complicated. Here's my code:
#%% Create a dataframe
import pandas as pd
values = {'Latitude': {0: 47.021503365600005,
1: 47.021503365600005,
2: 47.021503365600005,
3: 47.021503365600005,
4: 47.021503365600005,
5: 47.021503365600005},
'Longitude': {0: 15.481974060399999,
1: 15.481974060399999,
2: 15.481974060399999,
3: 15.481974060399999,
4: 15.481974060399999,
5: 15.481974060399999}}
df = pd.DataFrame(values)
df.head()
#%% Save the dataframe including a version number
import pickle
VERSION = 1
file_path = 'tmp.p'
with open(file_path, 'wb') as f:
pickle.dump((df, VERSION), f)
#%% Load the dataframe including the original verison number
try:
with open(file_path, 'r') as f:
df, version = pickle.load(f)
except ValueError as ex:
print (ex)
version = -1
#%% Compare version numbers
if version != VERSION:
print ('Version do not match')