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

Add levels property for MultiIndex #973

Merged
merged 10 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,32 @@ def __getattr__(self, item: str) -> Any:
def rename(self, name, inplace=False):
raise NotImplementedError()

@property
def levels(self) -> list:
"""
Names of index columns in list.

ueshin marked this conversation as resolved.
Show resolved Hide resolved
Examples:
--------
ueshin marked this conversation as resolved.
Show resolved Hide resolved
>>> mi = pd.MultiIndex.from_arrays((list('abc'), list('def')))
>>> mi.names = ['level_1', 'level_2']
>>> kdf = ks.DataFrame({'a': [1, 2, 3]}, index=mi)
>>> kdf.index.levels
[['a', 'b', 'c'], ['d', 'e', 'f']]

>>> mi = pd.MultiIndex.from_arrays((list('bac'), list('fee')))
>>> mi.names = ['level_1', 'level_2']
>>> kdf = ks.DataFrame({'a': [1, 2, 3]}, index=mi)
>>> kdf.index.levels
[['a', 'b', 'c'], ['e', 'f']]
"""
scols = self._kdf._internal.index_scols
sdf = self._kdf._sdf.select([F.collect_set(scol) for scol in scols]).collect()[0]
ueshin marked this conversation as resolved.
Show resolved Hide resolved

# use sorting is because pandas doesn't care the appearance order of level
# names, so e.g. if ['b', 'd', 'a'] will return as ['a', 'b', 'd']
return [sorted(col) for col in sdf]

def __repr__(self):
max_display_count = get_option("display.max_rows")
if max_display_count is None:
Expand Down
1 change: 0 additions & 1 deletion databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class _MissingPandasLikeMultiIndex(object):
T = unsupported_property('T')
codes = unsupported_property('codes')
is_all_dates = unsupported_property('is_all_dates')
levels = unsupported_property('levels')
levshape = unsupported_property('levshape')
shape = unsupported_property('shape')

Expand Down
12 changes: 12 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,15 @@ def test_multiindex_nlevel(self):
kdf = ks.from_pandas(pdf)

self.assertEqual(kdf.index.nlevels, 2)

def test_multiindex_levels(self):
tuples = [[list('abc'), list('def')], [list('aac'), list('fed')]]

for tup in tuples:
pdf = pd.DataFrame({'a': [1, 2, 3]}, index=tup)
kdf = ks.from_pandas(pdf)

# pandas returns FronzeList, so need to convert it to normal list
# for comparison
pdf_levels = [list(i) for i in pdf.index.levels]
self.assertEqual(pdf_levels, kdf.index.levels)