3

I'm getting an error if using a subclass of pd.Index which contains non-primitive values. I'm using pandas 0.13.1.

A quick demonstration:

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:

  1. Is this a bug or am I doing something wrong?
  2. What is the best way to fix/bypass this error, while still (a) using a subclass of pd.Index (my subclass includes an extended interface), and (b) contains non-primitive values (objects of a user-defined class).

1 Answer 1

1

You are using a fairly old version of pandas (current is 0.15.2). So Index is ultimately a sub-class of ndarray a pretty inflexible class to sub-class. Pandas changes to NOT sub-class this in 0.15.0.

Not sure of your goals here. Using a custom object index is pretty tricky to get right. Pandas has a new index type coming up in 0.16.0 (see here), as well as a whole panopoly of indexes that represent various types. Storing objects is not performant at all and is likely to cause great pain.

If you simply want to expand some aspect of custom usability it might be more ameanable to simple do this.

Sign up to request clarification or add additional context in comments.

1 Comment

I see it is only 10 month old. When working on a big complex system, it is too risky and time consuming to upgrade to a new version so frequently. I do appreciate the pace in which pandas is improving and evolving (seriously, it's awesome), but unfortunately, I can't afford to upgrade so frequently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.