Home > Back-end >  Prevent numpy object array from casting elements
Prevent numpy object array from casting elements

Time:04-12

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!

CodePudding user response:

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)],
                                 )        ]
  • Related