Skip to content

Commit 59ea171

Browse files
skvrahulluckyvs1
authored andcommitted
ENH: Add MultiIndex.dtypes (pandas-dev#37073)
1 parent 554b932 commit 59ea171

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

doc/source/reference/indexing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ MultiIndex properties
290290
MultiIndex.codes
291291
MultiIndex.nlevels
292292
MultiIndex.levshape
293+
MultiIndex.dtypes
293294

294295
MultiIndex components
295296
~~~~~~~~~~~~~~~~~~~~~

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Enhancements
1919
Other enhancements
2020
^^^^^^^^^^^^^^^^^^
2121

22-
-
22+
- Added :meth:`MultiIndex.dtypes` (:issue:`37062`)
2323
-
2424

2525
.. ---------------------------------------------------------------------------

pandas/core/indexes/multi.py

+9
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,15 @@ def array(self):
700700
"'MultiIndex.to_numpy()' to get a NumPy array of tuples."
701701
)
702702

703+
@cache_readonly
704+
def dtypes(self) -> "Series":
705+
"""
706+
Return the dtypes as a Series for the underlying MultiIndex
707+
"""
708+
from pandas import Series
709+
710+
return Series({level.name: level.dtype for level in self.levels})
711+
703712
@property
704713
def shape(self) -> Shape:
705714
"""

pandas/tests/indexes/multi/test_get_set.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
5+
46
import pandas as pd
57
from pandas import CategoricalIndex, MultiIndex
68
import pandas._testing as tm
@@ -27,6 +29,22 @@ def test_get_level_number_integer(idx):
2729
idx._get_level_number("fourth")
2830

2931

32+
def test_get_dtypes():
33+
# Test MultiIndex.dtypes (# Gh37062)
34+
idx_multitype = MultiIndex.from_product(
35+
[[1, 2, 3], ["a", "b", "c"], pd.date_range("20200101", periods=2, tz="UTC")],
36+
names=["int", "string", "dt"],
37+
)
38+
expected = pd.Series(
39+
{
40+
"int": np.dtype("int64"),
41+
"string": np.dtype("O"),
42+
"dt": DatetimeTZDtype(tz="utc"),
43+
}
44+
)
45+
tm.assert_series_equal(expected, idx_multitype.dtypes)
46+
47+
3048
def test_get_level_number_out_of_bounds(multiindex_dataframe_random_data):
3149
frame = multiindex_dataframe_random_data
3250

0 commit comments

Comments
 (0)