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

fix panel tranpose arguments api bug #3558

Merged
merged 1 commit into from
May 10, 2013
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
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pandas 0.11.1
e.g. add datetimes, multiple timedelta x datetime
- Fix ``.diff`` on datelike and timedelta operations (GH3100_)
- ``combine_first`` not returning the same dtype in cases where it can (GH3552_)
- Fixed bug with ``Panel.transpose`` argument aliases (GH3556_)

.. _GH3164: https://github.com/pydata/pandas/issues/3164
.. _GH2786: https://github.com/pydata/pandas/issues/2786
Expand Down Expand Up @@ -138,6 +139,7 @@ pandas 0.11.1
.. _GH3492: https://github.com/pydata/pandas/issues/3492
.. _GH3552: https://github.com/pydata/pandas/issues/3552
.. _GH3493: https://github.com/pydata/pandas/issues/3493
.. _GH3556: https://github.com/pydata/pandas/issues/3556


pandas 0.11.0
Expand Down
31 changes: 25 additions & 6 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,16 +1166,35 @@ def transpose(self, *args, **kwargs):
-------
y : Panel (new object)
"""

# construct the args
args = list(args)
aliases = tuple(kwargs.iterkeys())

for a in self._AXIS_ORDERS:
if not a in kwargs:
try:
kwargs[a] = args.pop(0)
except (IndexError):
raise ValueError(
"not enough arguments specified to transpose!")
where = map(a.startswith, aliases)

if any(where):
if sum(where) != 1:
raise AssertionError(
'Ambiguous parameter aliases "{0}" passed, valid '
'parameter aliases are '
'{1}'.format([n for n, m in zip(aliases, where)
if m], self._AXIS_ALIASES))

k = aliases[where.index(True)]

try:
kwargs[self._AXIS_ALIASES[k]] = kwargs.pop(k)
except KeyError:
raise KeyError('Invalid parameter alias '
'"{0}"'.format(k))
else:
try:
kwargs[a] = args.pop(0)
except IndexError:
raise ValueError(
"not enough arguments specified to transpose!")

axes = [self._get_axis_number(kwargs[a]) for a in self._AXIS_ORDERS]

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,30 @@ def test_transpose(self):
expected = self.panel.swapaxes('items', 'minor')
assert_panel_equal(result, expected)

# test kwargs
result = self.panel.transpose(items='minor', major='major',
minor='items')
expected = self.panel.swapaxes('items', 'minor')
assert_panel_equal(result, expected)

# text mixture of args
result = self.panel.transpose('minor', major='major', minor='items')
expected = self.panel.swapaxes('items', 'minor')
assert_panel_equal(result, expected)

result = self.panel.transpose('minor', 'major', minor='items')
expected = self.panel.swapaxes('items', 'minor')
assert_panel_equal(result, expected)

## test bad aliases
# test ambiguous aliases
self.assertRaises(AssertionError, self.panel.transpose, 'minor',
maj='major', majo='items')

# test invalid kwargs
self.assertRaises(KeyError, self.panel.transpose, 'minor',
maj='major', minor='items')

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

Expand Down