Without going into detail why I'd like to do this - is it possible to create a pandas MultiIndex from separate Index objects? I'm specifically interested in somehow preserving the specific Index types in the new MultiIndex instance. Solutions including conversions to and from a dataframe / series, etc. are fine as long as the original separate Index object types (RangeIndex, Int64Index, DatetimeIndex..) survive.
CodePudding user response:
You can create a MultiIndex
from multiple Index
es using pd.MultiIndex.from_arrays
:
>>> x = pd.RangeIndex(0, 10)
>>> y = pd.DatetimeIndex(pd.date_range('2020-04-01', '2020-04-10'))
>>> pd.MultiIndex.from_arrays([x, y])
MultiIndex([(0, '2020-04-01'),
(1, '2020-04-02'),
(2, '2020-04-03'),
(3, '2020-04-04'),
(4, '2020-04-05'),
(5, '2020-04-06'),
(6, '2020-04-07'),
(7, '2020-04-08'),
(8, '2020-04-09'),
(9, '2020-04-10')],
)