Skip to content

BUG: Bug in fully reindexing a Panel (GH5905) #5906

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

Merged
merged 1 commit into from
Jan 11, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Bug Fixes
- Bug in internal caching, related to (:issue:`5727`)
- Testing bug in reading json/msgpack from a non-filepath on windows under py3 (:issue:`5874`)
- Bug when assigning to .ix[tuple(...)] (:issue:`5896`)
- Bug in fully reindexing a Panel (:issue:`5905`)

pandas 0.13.0
-------------
Expand Down
37 changes: 2 additions & 35 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,41 +585,8 @@ def tail(self, n=5):
raise NotImplementedError

def _needs_reindex_multi(self, axes, method, level):
# only allowing multi-index on Panel (and not > dims)
return (method is None and
not self._is_mixed_type and
self._AXIS_LEN <= 3 and
com._count_not_none(*axes.values()) == 3)

def _reindex_multi(self, axes, copy, fill_value):
""" we are guaranteed non-Nones in the axes! """
items = axes['items']
major = axes['major_axis']
minor = axes['minor_axis']
a0, a1, a2 = len(items), len(major), len(minor)

values = self.values
new_values = np.empty((a0, a1, a2), dtype=values.dtype)

new_items, indexer0 = self.items.reindex(items)
new_major, indexer1 = self.major_axis.reindex(major)
new_minor, indexer2 = self.minor_axis.reindex(minor)

if indexer0 is None:
indexer0 = lrange(len(new_items))

if indexer1 is None:
indexer1 = lrange(len(new_major))

if indexer2 is None:
indexer2 = lrange(len(new_minor))

for i, ind in enumerate(indexer0):
com.take_2d_multi(values[ind], (indexer1, indexer2),
out=new_values[i])

return Panel(new_values, items=new_items, major_axis=new_major,
minor_axis=new_minor)
""" don't allow a multi reindex on Panel or above ndim """
return False

def dropna(self, axis=0, how='any', inplace=False, **kwargs):
"""
Expand Down
48 changes: 40 additions & 8 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,14 +1082,6 @@ def test_reindex(self):
result = self.panel.reindex(minor=new_minor)
assert_frame_equal(result['ItemB'], ref.reindex(columns=new_minor))

result = self.panel.reindex(items=self.panel.items,
major=self.panel.major_axis,
minor=self.panel.minor_axis)

self.assert_(result.items is self.panel.items)
self.assert_(result.major_axis is self.panel.major_axis)
self.assert_(result.minor_axis is self.panel.minor_axis)

# this ok
result = self.panel.reindex()
assert_panel_equal(result,self.panel)
Expand All @@ -1110,6 +1102,46 @@ def test_reindex(self):
assert_panel_equal(result,self.panel)
self.assert_((result is self.panel) == True)

def test_reindex_multi(self):

# with and without copy full reindexing
result = self.panel.reindex(items=self.panel.items,
major=self.panel.major_axis,
minor=self.panel.minor_axis,
copy = False)

self.assert_(result.items is self.panel.items)
self.assert_(result.major_axis is self.panel.major_axis)
self.assert_(result.minor_axis is self.panel.minor_axis)

result = self.panel.reindex(items=self.panel.items,
major=self.panel.major_axis,
minor=self.panel.minor_axis,
copy = False)
assert_panel_equal(result,self.panel)

# multi-axis indexing consistency
# GH 5900
df = DataFrame(np.random.randn(4,3))
p = Panel({ 'Item1' : df })
expected = Panel({ 'Item1' : df })
expected['Item2'] = np.nan

items = ['Item1','Item2']
major_axis = np.arange(4)
minor_axis = np.arange(3)

results = []
results.append(p.reindex(items=items, major_axis=major_axis, copy=True))
results.append(p.reindex(items=items, major_axis=major_axis, copy=False))
results.append(p.reindex(items=items, minor_axis=minor_axis, copy=True))
results.append(p.reindex(items=items, minor_axis=minor_axis, copy=False))
results.append(p.reindex(items=items, major_axis=major_axis, minor_axis=minor_axis, copy=True))
results.append(p.reindex(items=items, major_axis=major_axis, minor_axis=minor_axis, copy=False))

for i, r in enumerate(results):
assert_panel_equal(expected,r)

def test_reindex_like(self):
# reindex_like
smaller = self.panel.reindex(items=self.panel.items[:-1],
Expand Down