-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
ENH: Add allow_duplicates to MultiIndex.to_frame #45318
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
Changes from 5 commits
3194d97
a3359b9
670ecf5
025c9d6
61bff46
0444265
8052b13
31110eb
4c0b994
066c34f
dbca195
e69fc47
7f8fd32
e6f5894
29ac6b6
ed26844
3cfec79
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 | ||
---|---|---|---|---|
|
@@ -1710,7 +1710,12 @@ def unique(self, level=None): | |||
level = self._get_level_number(level) | ||||
return self._get_level_values(level=level, unique=True) | ||||
|
||||
def to_frame(self, index: bool = True, name=lib.no_default) -> DataFrame: | ||||
def to_frame( | ||||
self, | ||||
index: bool = True, | ||||
name=lib.no_default, | ||||
allow_duplicates: bool = False, | ||||
) -> DataFrame: | ||||
""" | ||||
Create a DataFrame with the levels of the MultiIndex as columns. | ||||
|
||||
|
@@ -1725,6 +1730,11 @@ def to_frame(self, index: bool = True, name=lib.no_default) -> DataFrame: | |||
name : list / sequence of str, optional | ||||
The passed names should substitute index level names. | ||||
|
||||
allow_duplicates : bool, optional default False | ||||
Allow duplicate column labels to be created. | ||||
|
||||
.. versionadded:: 1.5.0 | ||||
|
||||
Returns | ||||
------- | ||||
DataFrame : a DataFrame containing the original MultiIndex data. | ||||
|
@@ -1774,14 +1784,21 @@ def to_frame(self, index: bool = True, name=lib.no_default) -> DataFrame: | |||
else: | ||||
idx_names = self.names | ||||
|
||||
idx_names = [ | ||||
|
name = self.name or 0 |
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.
yeah i think a common method on index is worth it here (to share here & reset_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.
Ok, but I have already explained that reset_index
does not do this.
reset_index
, to_records
and many other methods all fill the None entries with "level_n", not with "n". As you know, I factored those out into a common method (com.fill_missing_names
#44878) which is invoked in 6 different places.
These MI/Index.to_frame
methods are the only ones which do it differently, filling the gaps with the column number. This difference could be discussed, and I have made an issue (#45245), but I don't suggest changing it without a lot of thought. Changing to_frame
would break virtually all its tests.
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.
revert this
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.
Done