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!
[a,b]? What does object array do for you?array.shape?(1,). But it might also be(30,30,20)a.valuesis 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 normalnp.arraybehavior.