Skip to content

Commit 3540650

Browse files
RainFungueshin
authored andcommitted
Implement MultiIndex.levshape (#1086)
Get MultiIndex unique value count in every level.
1 parent 7844193 commit 3540650

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

databricks/koalas/indexes.py

+21
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,27 @@ def name(self) -> str:
861861
def name(self, name: str) -> None:
862862
raise PandasNotImplementedError(class_name='pd.MultiIndex', property_name='name')
863863

864+
@property
865+
def levshape(self):
866+
"""
867+
A tuple with the length of each level.
868+
869+
Examples
870+
--------
871+
>>> midx = ks.MultiIndex.from_tuples([('a', 'x'), ('b', 'y'), ('c', 'z')])
872+
>>> midx # doctest: +SKIP
873+
MultiIndex([('a', 'x'),
874+
('b', 'y'),
875+
('c', 'z')],
876+
)
877+
878+
>>> midx.levshape
879+
(3, 3)
880+
"""
881+
internal = self._internal
882+
result = internal._sdf.agg(*(F.countDistinct(c) for c in internal.index_scols)).collect()[0]
883+
return tuple(result)
884+
864885
def to_pandas(self) -> pd.MultiIndex:
865886
"""
866887
Return a pandas MultiIndex.

databricks/koalas/missing/indexes.py

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class _MissingPandasLikeMultiIndex(object):
118118

119119
# Properties
120120
is_all_dates = unsupported_property('is_all_dates')
121-
levshape = unsupported_property('levshape')
122121

123122
# Deprecated properties
124123
strides = unsupported_property('strides', deprecated=True)

databricks/koalas/tests/test_indexes.py

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ def test_multi_index_names(self):
150150
with self.assertRaises(PandasNotImplementedError):
151151
kidx.name = 'renamed'
152152

153+
def test_multi_index_levshape(self):
154+
pidx = pd.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2)])
155+
kidx = ks.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2)])
156+
self.assertEqual(pidx.levshape, kidx.levshape)
157+
153158
def test_index_unique(self):
154159
kidx = self.kdf.index
155160

docs/source/reference/indexing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ MultiIndex Properties
101101
MultiIndex.ndim
102102
MultiIndex.T
103103
MultiIndex.nlevels
104+
MultiIndex.levshape
104105

105106
MultiIndex Missing Values
106107
~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)