-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
API: Have MultiIndex consturctors always return a MI #17236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from pandas.core.indexes.base import (Index, _new_Index, # noqa | ||
_ensure_index, _get_na_value, | ||
InvalidIndexError) | ||
from pandas.core.indexes.base import (Index, | ||
_new_Index, | ||
_ensure_index, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this shouldn't need a noqa (if its line length, break it on the prarens There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some of these are unused and just there to export as part of the API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k |
||
_ensure_index_from_sequences, | ||
_get_na_value, | ||
InvalidIndexError) # noqa | ||
from pandas.core.indexes.category import CategoricalIndex # noqa | ||
from pandas.core.indexes.multi import MultiIndex # noqa | ||
from pandas.core.indexes.interval import IntervalIndex # noqa | ||
|
@@ -22,7 +25,8 @@ | |
'InvalidIndexError', 'TimedeltaIndex', | ||
'PeriodIndex', 'DatetimeIndex', | ||
'_new_Index', 'NaT', | ||
'_ensure_index', '_get_na_value', '_get_combined_index', | ||
'_ensure_index', '_ensure_index_from_sequences', '_get_na_value', | ||
'_get_combined_index', | ||
'_get_objs_combined_axis', '_union_indexes', | ||
'_get_consensus_names', | ||
'_all_indexes_same'] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
|
||
from pandas.core.frame import _shared_docs | ||
from pandas.util._decorators import Appender | ||
from pandas.core.index import MultiIndex, _get_na_value | ||
from pandas.core.index import Index, MultiIndex, _get_na_value | ||
|
||
|
||
class _Unstacker(object): | ||
|
@@ -311,10 +311,14 @@ def _unstack_multiple(data, clocs): | |
recons_labels = decons_obs_group_ids(comp_ids, obs_ids, shape, clabels, | ||
xnull=False) | ||
|
||
dummy_index = MultiIndex(levels=rlevels + [obs_ids], | ||
labels=rlabels + [comp_ids], | ||
names=rnames + ['__placeholder__'], | ||
verify_integrity=False) | ||
if rlocs == []: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a comment on why the if is needed |
||
# Everything is in clocs, so the dummy df has a regular index | ||
dummy_index = Index(obs_ids, name='__placeholder__') | ||
else: | ||
dummy_index = MultiIndex(levels=rlevels + [obs_ids], | ||
labels=rlabels + [comp_ids], | ||
names=rnames + ['__placeholder__'], | ||
verify_integrity=False) | ||
|
||
if isinstance(data, Series): | ||
dummy = data.copy() | ||
|
@@ -446,7 +450,12 @@ def _slow_pivot(index, columns, values): | |
|
||
def unstack(obj, level, fill_value=None): | ||
if isinstance(level, (tuple, list)): | ||
return _unstack_multiple(obj, level) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a comment on why this if is needed here |
||
if len(level) != 1: | ||
# _unstack_multiple only handles MultiIndexes, | ||
# and isn't needed for a single level | ||
return _unstack_multiple(obj, level) | ||
else: | ||
level = level[0] | ||
|
||
if isinstance(obj, DataFrame): | ||
if isinstance(obj.index, MultiIndex): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1452,7 +1452,12 @@ def cons_row(x): | |
|
||
if expand: | ||
result = list(result) | ||
return MultiIndex.from_tuples(result, names=name) | ||
out = MultiIndex.from_tuples(result, names=name) | ||
if out.nlevels == 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a comment on why this is needed |
||
# We had all tuples of length-one, which are | ||
# better represented as a regular Index. | ||
out = out.get_level_values(0) | ||
return out | ||
else: | ||
return Index(result, name=name) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1909,7 +1909,11 @@ def keyfunc(x): | |
|
||
# convert tuples to index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment here |
||
if nentries == 1: | ||
# we have a single level of tuples, i.e. a regular Index | ||
index = Index(tuples[0], name=names[0]) | ||
elif nlevels == 1: | ||
name = None if names is None else names[0] | ||
index = Index((x[0] for x in tuples), name=name) | ||
else: | ||
index = MultiIndex.from_tuples(tuples, names=names) | ||
return index | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same (or at least that's the intent); otherwise maybe should have an option / another helper function to avoid this repetition of code.