get the dtype of a pandas multiindex
Use get_level_values:
df.index.get_level_values(0).dtype
dtype('int64')
and
df.index.get_level_values(1).dtype
dtype('int64')
For names use:
df.index.names
FrozenList(['i1', 'i2'])
Based on the great answer by @Scott Boston, I wrote this function for a quick check for the dtypes of all the levels of multiindex dfs and just thought I'd share:
def index_level_dtypes(df):
return [f"{df.index.names[i]}: {df.index.get_level_values(n).dtype}"
for i, n in enumerate(df.index.names)]
pandas >= 1.3 [est]
Coming soon to a distro near you, you MultiIndex.dtypes
will soon be available:
df.index.dtypes
i1 int64
i2 int64
dtype: object
For older versions, if your index is not too large, you can first convert to a frame using to_frame
, then query the dtypes:
df.index.to_frame().dtypes
i1 int64
i2 int64
dtype: object