-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: MultiIndex dtypes incorrect if level names not unique #45175
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 2 commits
da75bbe
a95f3b3
4bd8156
7777a8d
a8111de
eb314d0
71f577c
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 |
---|---|---|
|
@@ -735,13 +735,14 @@ def array(self): | |
def dtypes(self) -> Series: | ||
""" | ||
Return the dtypes as a Series for the underlying MultiIndex. | ||
|
||
.. versionchanged:: 1.4.0 | ||
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 don't think that we need this, since this qualifies as a bug fix 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. agree 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. Done |
||
Correct result when there are duplicated level names. | ||
""" | ||
from pandas import Series | ||
|
||
names = com.fill_missing_names([level.name for level in self.levels]) | ||
return Series( | ||
{names[idx]: level.dtype for idx, level in enumerate(self.levels)} | ||
) | ||
return Series([level.dtype for level in self.levels], index=names) | ||
|
||
def __len__(self) -> int: | ||
return len(self.codes[0]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,13 @@ def test_get_dtypes_no_level_name(): | |
tm.assert_series_equal(expected, idx_multitype.dtypes) | ||
|
||
|
||
def test_get_dtypes_duplicate_level_names(): | ||
# Test MultiIndex.dtypes with non-unique level names (# GH45174 ) | ||
result = MultiIndex.from_arrays([[1], [2]], names=[1, 1]).dtypes | ||
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. Could you also add a test with different dtypes? 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. |
||
expected = pd.Series([np.dtype("int64"), np.dtype("int64")], index=[1, 1]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_get_level_number_out_of_bounds(multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
|
||
|
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.
Could you clarify a bit? Maybe only one entry for all levels with same name. I though this would return a Series with the correct length but with the same dtype in every row
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.
Hopefully done.