-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Bugfix for multilevel columns with empty strings in Python 2 #17099
Changes from 5 commits
e54c734
a798b70
63071bd
e7c9f97
b419cb6
f1edf68
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 |
---|---|---|
|
@@ -2134,10 +2134,18 @@ def _getitem_multilevel(self, key): | |
result = self._constructor(new_values, index=self.index, | ||
columns=result_columns) | ||
result = result.__finalize__(self) | ||
|
||
# If there is only one column being returned, and its name is | ||
# either an empty string, or a tuple with an empty string as its | ||
# first element, then treat the empty string as a placeholder | ||
# and return the column as if the user had provided that empty | ||
# string in the key. If the result is a Series, exclude the | ||
# implied empty string from its name. | ||
if len(result.columns) == 1: | ||
top = result.columns[0] | ||
if ((type(top) == str and top == '') or | ||
(type(top) == tuple and top[0] == '')): | ||
if isinstance(top, tuple): | ||
top = top[0] | ||
if top == '': | ||
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. put a comment what is going on here. |
||
result = result[''] | ||
if isinstance(result, Series): | ||
result = self._constructor_sliced(result, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1675,24 +1675,34 @@ def test_int_series_slicing(self): | |
expected = self.ymd.reindex(s.index[5:]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_mixed_depth_get(self): | ||
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. use
|
||
def test_mixed_depth_get(self, unicode_strings_py2=False): | ||
# If unicode_strings_py2 is True, then the column labels in dataframe | ||
# construction will use unicode strings in Python 2. In Python 3 they | ||
# are unicode strings regardless. | ||
|
||
arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'], | ||
['', 'OD', 'OD', 'result1', 'result2', 'result1'], | ||
['', 'wx', 'wy', '', '', '']] | ||
|
||
if unicode_strings_py2: | ||
arrays = [[u(s) for s in arr] for arr in arrays] | ||
|
||
tuples = sorted(zip(*arrays)) | ||
index = MultiIndex.from_tuples(tuples) | ||
df = DataFrame(randn(4, 6), columns=index) | ||
df = DataFrame(np.random.randn(4, 6), columns=index) | ||
|
||
result = df['a'] | ||
expected = df['a', '', ''] | ||
tm.assert_series_equal(result, expected, check_names=False) | ||
assert result.name == 'a' | ||
expected = df['a', '', ''].rename('a') | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df['routine1', 'result1'] | ||
expected = df['routine1', 'result1', ''] | ||
tm.assert_series_equal(result, expected, check_names=False) | ||
assert result.name == ('routine1', 'result1') | ||
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 parametrize |
||
expected = expected.rename(('routine1', 'result1')) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_mixed_depth_get_unicode_placeholders_py2(self): | ||
# Pull request #17099. | ||
self.test_mixed_depth_get(unicode_strings_py2=True) | ||
|
||
def test_mixed_depth_insert(self): | ||
arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'], | ||
|
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.
this is a mouthful, can you simplify a bit