1

Good morning!

I'm using a code (with python 3.8) that is running both in a local PC and in a ssh server. In one point, I'm loading data from a pickle using the next piece of code:

from os.path  import exists
import _pickle as pickle

def load_pickle(pickle_file):

    if exists(pickle_file):
    
        with open(pickle_file, 'rb') as f:
            loaded_dic = pickle.load(f)

        return loaded_dic

    else:
        return 'Pickle not found'

pickle_file is a string with the path of the pickle. If the pickle exists, the function returns a dictionary, while if it doesn't exist, it returns the string 'Pickle not found'.

In my local PC, the code works perfectly, loading the dict without problems. However, in the ssh server, theoretically, the dict is loaded, but, if I try to access to it, jus typing loaded_dic, it throws the following error:

AttributeError: 'NoneType' object has no attribute 'axes'

Due to it, the rest of my code fails when it try to use the variable loaded_dic.

Thank you very much in advance!

Kenster
  • 18,710
  • 21
  • 68
  • 90
Á. Garzón
  • 157
  • 7

3 Answers3

7

I have a similar problem. For me it happens as I store pandas DataFrames in a dictionary and save this dict as a pickle with pandas version '1.1.1'.

When I read the dictionary pickle with pandas version '0.25.3' on another server, I get the same error.

Both have pickle version 4.0 and I do not have a solution yet, other than upgrading to similar pandas versions.

I made a small example, it also happens when I store just a DataFrame, Saving it on one machine:

import pandas as pd
print("Pandas version", pd.__version__)

df = pd.DataFrame([1, 2, 3])

df.to_pickle('df.pkl')

Pandas version 1.1.1

Then loading it on another machine:

import pandas as pd
print("Pandas version", pd.__version__)

df = pd.read_pickle('df.pkl')

print(type(df))

Pandas version 0.25.3
<class 'pandas.core.frame.DataFrame'>

print(len(df))

results in this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-9f6b6d8c3cd3> in <module>
----> 1 print(len(df))

/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __len__(self)
    994         Returns length of info axis, but here we use the index.
    995         """
--> 996         return len(self.index)
    997 
    998     def dot(self, other):

/opt/conda/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5173             or name in self._accessors
   5174         ):
-> 5175             return object.__getattribute__(self, name)
   5176         else:
   5177             if self._info_axis._can_hold_identifiers_and_holds_name(name):

pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__get__()

AttributeError: 'NoneType' object has no attribute 'axes'
Sheldon
  • 454
  • 1
  • 4
  • 12
  • 1
    I have the same problem with the same cause. – rigo Oct 27 '20 at 17:13
  • I think your error is due to the different version of Pandas on the different machines – Avi Vajpeyi Jan 28 '21 at 14:20
  • Got the same error. I first installed pandas using `sudo apt install python3-pandas` which is version 0.25.3. I solved the problem by removing the old version `sudo apt remove python3-pandas` and installing the latest version `pip3 install pandas`. – Zheng Xiaochen May 03 '21 at 10:20
1

This code clearly doesn't work anywhere; it doesn't return loaded_dict, a local variable, so nothing can use it. Change it to:

        return pickle.load(f)

and the caller will receive the loaded dict instead of the default return value, None.

Update for edited question: With the return, your code works as written. Your pickle file on said machine must have the result of pickling None stored in it, rather than whatever you expected. Or your code is broken in some other place we haven't seen. The loading code is fine, and behaving exactly as its supposed to.

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
  • Sorry, that was a writing mistake. The code has the corresponding return. – Á. Garzón Sep 09 '20 at 13:23
  • @Á.Garzón: Well then the code you're running isn't the code you posted, and/or the pickle file encodes a `None`. Or you forgot to call your function. Or something else ridiculous; if you don't post a true [MCVE], it's impossible to help you. – ShadowRanger Sep 09 '20 at 13:25
  • I think the problem can come from the pickle package. Anyway, n any case, contrary to what many users of this forum thin, no matter how good your scores are, it is not always possible to create an example from codes of more than 3000 lines and the need for access to private business databases. – Á. Garzón Sep 09 '20 at 13:36
0

Avi's answer helped me. I had pickled with a later version of Pandas and was trying to read the pickle file with an earlier version.