0

I am trying to construct a numpy array of pandas MultiIndexes. The array has dtype='object' and each element of the array should be a pd.MultiIndex.

Here's an example:

a  = pd.MultiIndex.from_product([pd.RangeIndex(3), pd.RangeIndex(2)])
b  = pd.MultiIndex.from_product([pd.RangeIndex(3), pd.RangeIndex(2)])
array = np.array([a,b], dtype='object')

The output is:

array([[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)],
       [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]], dtype=object)

What i would like the output to be:

array([MultiIndex([(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]),
       MultiIndex([(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)])], dtype=object)

I would assume that specifying dtype='object' when creating the array would prevent numpy from casting the input to some other python type, but apparently it is casting my multiindexes into list. How do i stop it from casting?

Thanks!

5
  • why not just use a list [a,b]? What does object array do for you? Commented Apr 8, 2022 at 9:49
  • I need the array because i need to use a lot of numpy-specific tools on it, like flatten and reshape. This array is used to construct another 'real' numpy array. If i could use a normal list I would, believe me. Commented Apr 8, 2022 at 9:52
  • what's array.shape? Commented Apr 8, 2022 at 10:05
  • That will depend on the input data. Could be as easy as (1,). But it might also be (30,30,20) Commented Apr 8, 2022 at 10:18
  • I wanted the shape of the actual array you show, I was puzzled by the tuples in what looks like a (2,6) array. Why wasn't it (2,6,2). Turns out a.values is a 1d object dtype array of tuples. If they differed in length it would keep them separate, but since they match it can make a 2d array instad. That's normal np.array behavior. Commented Apr 8, 2022 at 16:15

1 Answer 1

2

You can use np.empty to create an empty array of the desired shape and then copy the data to the array:

lst = [a, b]
array = np.empty(len(lst), dtype=object)
array[:] = lst[:]
[MultiIndex([(0, 0),
             (0, 1),
             (1, 0),
             (1, 1),
             (2, 0),
             (2, 1)],
            )         MultiIndex([(0, 0),
                                  (0, 1),
                                  (1, 0),
                                  (1, 1),
                                  (2, 0),
                                  (2, 1)],
                                 )        ]
Sign up to request clarification or add additional context in comments.

Comments

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.