Skip to content

Commit

Permalink
ENH: copy keyword to Panel swapaxes/transpose #695
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang She committed May 4, 2012
1 parent 47abe8c commit 40fb1bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
13 changes: 9 additions & 4 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def groupby(self, function, axis='major'):
axis = self._get_axis_number(axis)
return PanelGroupBy(self, function, axis=axis)

def swapaxes(self, axis1='major', axis2='minor'):
def swapaxes(self, axis1='major', axis2='minor', copy=True):
"""
Interchange axes and swap values axes appropriately
Expand All @@ -930,11 +930,14 @@ def swapaxes(self, axis1='major', axis2='minor'):

new_axes = (self._get_axis(mapping.get(k, k))
for k in range(3))
new_values = self.values.swapaxes(i, j).copy()
new_values = self.values.swapaxes(i, j)
if copy:
new_values = new_values.copy()

return self._constructor(new_values, *new_axes)

def transpose(self, items='items', major='major', minor='minor'):
def transpose(self, items='items', major='major', minor='minor',
copy=True):
"""
Permute the dimensions of the Panel
Expand All @@ -948,7 +951,9 @@ def transpose(self, items='items', major='major', minor='minor'):
raise ValueError('Must specify 3 unique axes')

new_axes = [self._get_axis(x) for x in [i, j, k]]
new_values = self.values.transpose((i, j, k)).copy()
new_values = self.values.transpose((i, j, k))
if copy:
new_values = new_values.copy()
return self._constructor(new_values, *new_axes)

def to_frame(self, filter_observations=True):
Expand Down
26 changes: 12 additions & 14 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,17 @@ def test_swapaxes(self):
result = self.panel.swapaxes('major', 'minor')
self.assert_(result.major_axis is self.panel.minor_axis)

panel = self.panel.copy()
result = panel.swapaxes('major', 'minor')
panel.values[0, 0, 1] = np.nan
expected = panel.swapaxes('major', 'minor')
assert_panel_equal(result, expected)

# this should also work
result = self.panel.swapaxes(0, 1)
self.assert_(result.items is self.panel.major_axis)

# this should also work
# this should not work
self.assertRaises(Exception, self.panel.swapaxes, 'items', 'items')

def test_transpose(self):
Expand All @@ -911,22 +917,14 @@ def test_transpose(self):
result = self.panel.transpose(2, 0, 1)
assert_panel_equal(result, expected)

def test_transpose_sparse(self):
spanel = self.panel.to_sparse()
result = spanel.transpose('minor', 'major', 'items')
expected = spanel.swapaxes('items', 'minor')
assert_panel_equal(result, expected)

result = spanel.transpose(2, 1, 0)
assert_panel_equal(result, expected)

result = spanel.transpose('minor', 'items', 'major')
expected = spanel.swapaxes('items', 'minor')
panel = self.panel.copy()
result = panel.transpose(2, 0, 1, copy=False)
panel.values[0, 0, 1] = np.nan
expected = panel.swapaxes('items', 'minor')
expected = expected.swapaxes('major', 'minor')
assert_panel_equal(result, expected)

result = spanel.transpose(2, 0, 1)
assert_panel_equal(result, expected)
self.assertRaises(ValueError, self.panel.transpose, 0, 0, 1)

def test_to_frame(self):
# filtered
Expand Down

0 comments on commit 40fb1bf

Please sign in to comment.