Home > OS >  Python Pandas: itemsize functions throws AttributeError: 'Series' object has no attribute
Python Pandas: itemsize functions throws AttributeError: 'Series' object has no attribute

Time:02-05

I am new to Python Pandas, and, as part of that, I wanted to create a pandas.Series and print the itemsize of the Series. However, while I was doing that, it returned an attribute error.

Following is my code:

import pandas as pd
data = [1, 2, 3, 4]
s = pd.Series(data)
print(s.itemsize)

This however does not return the itemsize and throws the following error:

Traceback (most recent call last):
  File "c:\Users\USER\filename.py", line 32, in <module>
    print(s.itemsize)
          ^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py", line 5902, in __getattr__
    return object.__getattribute__(self, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Series' object has no attribute 'itemsize'```

What might the reason for this be?

CodePudding user response:

You probably confused it with numpy.ndarray.itemsize in .

In pandas, it's pandas.Series.dtype.itemsize :

import pandas as pd

data = [1, 2, 3, 4]
s = pd.Series(data)

Output :

print(s.dtype.itemsize)
#8
  • Related