Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pympler raises TypeError

In python2.7, following the pympler example:

from anotherfile import somefunction, somecustomclass
from os import path, listdir
import pandas as pd
import gc
from pympler import tracker, muppy, summary

all_objects = muppy.get_objects()
print 'all objects: ', len(all_objects)
sum1 = summary.summarize(all_objects)
summary.print_(sum1)

This is the first code after the imports. It results in

/usr/bin/python2.7 /myprog.py
all objects:  98755
Traceback (most recent call last):
File "/myprog.py", line 12, in <module>
sum1 = summary.summarize(all_objects)
File "/usr/local/lib/python2.7/dist-packages/pympler/summary.py", line 131, in summarize
total_size[otype] = _getsizeof(o)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/base.py", line 130, in __sizeof__
return super(self, PandasObject).__sizeof__()
TypeError: super() argument 1 must be type, not FrozenList

Process finished with exit code 1

I get the same error when I try to initialize a SummaryTracker object.

It looks like a bug in Pympler, but the fact that I can't find any mentions of it contradicts this. According to the official documentation, "Pympler is written entirely in Python, with no dependencies to external libraries. It has been tested with Python 2.5, 2.6, 2.7, 3.1, 3.2, 3.3, 3.4 on Linux, Windows and MacOS X." In fact, running only the following code with python 2.7 in a new python file does not produce any errors and works as expected:

from pympler import muppy, tracker

tr = tracker.SummaryTracker()
tr.print_diff()

So what am I missing?

like image 213
Darina Avatar asked Jun 03 '16 11:06

Darina


People also ask

What causes TypeError + (addition) to be raised?

For example, using the + (addition) operator on a string and an integer value will raise TypeError. The general causes for TypeError being raised are: 1. Unsupported operation between two types:

What is TypeError in Python?

Last Updated : 20 Aug, 2020 TypeError is one among the several standard Python exceptions. TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise TypeError.

Why does Python still generate TypeError when we convert list to string?

So when we execute it, python still generates TypeError, as it says element index 2, i.e. the third element is still an integer. So now we have to make the rest two elements also as integers to work properly. In the above program, you can see inside the join function, we have converted each element of list1 into a string by typecasting.

What are the causes of TypeError?

The general causes for TypeError being raised are: 1. Unsupported operation between two types: In the following example, the variable ‘geek’ is a string and the variable ‘num’ is an integer. The + (addition) operator cannot be used between these two types and hence TypeError is raised.


2 Answers

it seems to be a problem in pandas library. I solved it by editing the library code. The track of the error indicates you which line is wrong:

File "/usr/local/lib/python2.7/dist-packages/pandas/core/base.py", line 130, in __sizeof__ 
return super(self, PandasObject).__sizeof__()

You just have to change the order of the parameters like this:

return super(PandasObject, self).__sizeof__()

I did it and I was able to run normally my program.

like image 168
Nacho Traverso Ribón Avatar answered Sep 30 '22 04:09

Nacho Traverso Ribón


This is Pandas issue #12924. PandasObject.__sizeof__ had the arguments in the wrong order for the super call. The fix has been pulled, and it should be available in the next release. In the meantime, you could edit pandas/core/base.py to switch the argument order, or you could test for the bug's presence and monkey-patch the method with a corrected version.

like image 44
user2357112 supports Monica Avatar answered Sep 30 '22 05:09

user2357112 supports Monica