"maximum recursion depth" when subclassing pandas.Index
I am getting an error when using a subclass pd.Index
that contains non-primitive values. I am using pandas 0.13.1
.
Quick demo:
import pandas as pd
class MyIndex(pd.Index): pass
# MyIndex = pd.Index <-- if not subclassing, no problem
o1 = object(); o2 = object() # <-- if using primitives, no problem
j = MyIndex([ o1, o2 ])
i = pd.Index(j)
j2 = MyIndex([ o2 ])
i2 = pd.Index(j2)
try:
print pd.Series([ 4,5 ], index = j)[j2] # <-- RuntimeError: maximum recursion depth exceeded while calling a Python object
except RuntimeError, e:
print e
print pd.Series([ 4,5 ], index = j)[i2] # <-- works as expected
print pd.Series([ 4,5 ], index = i)[j2] # <-- works as expected
Questions:
- Is this a bug or am I doing something wrong?
- What is the best way to fix / work around this error and also (a) use a subclass
pd.Index
(my subclass includes an extended interface) and (b) contains non-primitive (objects of a specific class).
source to share
You are using a fairly old version of pandas (current is 0.15.2). So Index
ultimately subclassing is a ndarray
pretty inflexible class to subclass. pandas changes not to subclass the NOT class in 0.15.0.
Not sure about your goals here. Using a custom object index is pretty tricky to get right. pandas has a new index type starting at 0.16.0 (see here ), as well as a whole panorama of indexes representing different types.Object storage is not done at all and can be very painful.
If you just want to expand on some aspect of user usability, it might be easier for a simple this .
source to share