Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add MultiIndex.dtypes #37073

Merged
merged 15 commits into from
Dec 11, 2020
Merged
1 change: 1 addition & 0 deletions doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ MultiIndex properties
MultiIndex.codes
MultiIndex.nlevels
MultiIndex.levshape
MultiIndex.dtypes

MultiIndex components
~~~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ Other enhancements
- Calling a NumPy ufunc on a ``DataFrame`` with extension types now preserves the extension types when possible (:issue:`23743`).
- Calling a binary-input NumPy ufunc on multiple ``DataFrame`` objects now aligns, matching the behavior of binary operations and ufuncs on ``Series`` (:issue:`23743`).
- Where possible :meth:`RangeIndex.difference` and :meth:`RangeIndex.symmetric_difference` will return :class:`RangeIndex` instead of :class:`Int64Index` (:issue:`36564`)
- Added :meth:`MultiIndex.dtypes` (:issue:`37062`)
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
- :meth:`DataFrame.to_parquet` now supports :class:`MultiIndex` for columns in parquet format (:issue:`34777`)
- Added :meth:`.Rolling.sem` and :meth:`Expanding.sem` to compute the standard error of the mean (:issue:`26476`)
- :meth:`.Rolling.var` and :meth:`.Rolling.std` use Kahan summation and Welford's Method to avoid numerical issues (:issue:`37051`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,15 @@ def array(self):
"'MultiIndex.to_numpy()' to get a NumPy array of tuples."
)

@cache_readonly
def dtypes(self):
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
"""
Return the dtypes as a Series for the underlying MultiIndex
"""
from pandas import Series

return Series({level.name: level.dtype for level in self.levels})

@property
def shape(self) -> Shape:
"""
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/multi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def idx():
return mi


@pytest.fixture
def idx_multitype():
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
# a MultiIndex with several dtypes
first_axis = [1, 2, 3]
second_axis = list("abc")
third_axis = pd.date_range("20200101", periods=2, tz="UTC")
mi = MultiIndex.from_product(
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
[first_axis, second_axis, third_axis], names=["int", "string", "dt"]
)
return mi


@pytest.fixture
def idx_dup():
# compare tests/indexes/multi/conftest.py
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas.core.dtypes.dtypes import DatetimeTZDtype as DateTimeTZDtype
skvrahul marked this conversation as resolved.
Show resolved Hide resolved

import pandas as pd
from pandas import CategoricalIndex, MultiIndex
import pandas._testing as tm
Expand All @@ -27,6 +29,18 @@ def test_get_level_number_integer(idx):
idx._get_level_number("fourth")


def test_get_dtypes(idx_multitype):
# Test MultiIndex.dtypes (GH-37062)
skvrahul marked this conversation as resolved.
Show resolved Hide resolved
expected = pd.Series(
{
"int": np.dtype("int64"),
"string": np.dtype("O"),
"dt": DateTimeTZDtype(tz="utc"),
}
)
assert expected.equals(idx_multitype.dtypes)
skvrahul marked this conversation as resolved.
Show resolved Hide resolved


def test_get_level_number_out_of_bounds(multiindex_dataframe_random_data):
frame = multiindex_dataframe_random_data

Expand Down