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

Bugfix for multilevel columns with empty strings in Python 2 #17099

Merged
merged 6 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,8 +2112,9 @@ def _getitem_multilevel(self, key):
result = result.__finalize__(self)
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 == '':
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,28 @@ def test_mixed_depth_get(self):
tm.assert_series_equal(result, expected, check_names=False)
assert result.name == ('routine1', 'result1')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parametrize test_mixed_depth_get in that case then, rather than duping all of the code


def test_mixed_depth_get_unicode_placeholders_py2(self):
# Note this is only different to test_mixed_depth_get() on Python 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the PR number for this (as no associated issue number)

arrays = [[u('a'), u('top'), u('top'),
u('routine1'), u('routine1'), u('routine2')],
[u(''), u('OD'), u('OD'),
u('result1'), u('result2'), u('result1')],
[u(''), u('wx'), u('wy'), u(''), u(''), u('')]]

tuples = sorted(zip(*arrays))
index = MultiIndex.from_tuples(tuples)
df = DataFrame(randn(4, 6), columns=index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use np.random.randn


result = df['a']
expected = df['a', '', '']
tm.assert_series_equal(result, expected, check_names=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

construct the expected to have the correct names

assert result.name == 'a'

result = df['routine1', 'result1']
expected = df['routine1', 'result1', '']
tm.assert_series_equal(result, expected, check_names=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

assert result.name == ('routine1', 'result1')

def test_mixed_depth_insert(self):
arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
['', 'OD', 'OD', 'result1', 'result2', 'result1'],
Expand Down