From df79d053d5d586c00cd51328f48e25370e83f207 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Thu, 9 May 2013 19:56:01 -0400 Subject: [PATCH] BUG: fix panel.transpose arguments API bug only show the ambiguous aliases as opposed to all key-value pairs add release note --- RELEASE.rst | 2 ++ pandas/core/panel.py | 31 +++++++++++++++++++++++++------ pandas/tests/test_panel.py | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/RELEASE.rst b/RELEASE.rst index 8e48395efc9ab..89f7fcc6fdd37 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -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 @@ -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 diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 8d6828421c3fa..869bb31acad6b 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -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] diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 437f8b7279824..081af101b643b 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -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)