So I have a DB with a couple of years worth of site data. I am now attempting to use that data for analytics - plotting and sorting of advertising costs by keyword, etc.
One of the data grabs from the DB takes minutes to complete. While I could spend some time optimizing the SQL statements I use to get the data I'd prefer to simply leave that class and it's SQL alone, grab the data, and save the results to a data file for faster retrieval later. Most of this DB data isn't going to change so I could write a separate python script to update the file every 24 hours and then use that file for this long running task.
The data is being returned as a dictionary of numpy arrays. When I use numpy.save('data', data) the file is saved just fine. When I use data2 = numpy.load('data.npy') it loads the file without error. However, the output data2 doesn't not equal the original data.
Specifically the line data == data2 returns false. Additionally, if I use the following:
for key, key_data in data.items():
print key
it works. But when I replace data.items() with data2.items() then I get an error:
AttributeError: 'numpy.ndarray' object has no attribute 'items'
Using type(data) I get dict. Using type(data2) I get numpy.ndarray.
So how do I fix this? I want the loaded data to equal the data I passed in for saving. Is there an argument to numpy.save to fix this or do I need some form of simple reformatting function to reformat the loaded data into the proper structure?
Attempts to get into the ndarray via for loops or indexing all lead to errors about indexing a 0-d array. Casting like this dict(data2) also fails for iterating over a 0-d array. However, Spyder shows value of the array and it includes the data I saved. I just can't figure out how to get to it.
If I need to reformat the loaded data I'd appreciate some example code on how to do this.
np.savez('arrs', **my_dict)is simpler to use thannp.save(my_dict)for a flat dict of arrays, because the result ofnp.load('arrs.npz')can be directly indexed like a Python dict.