From 810cf9fc339b7978e2b1083b48131b2be4b247d5 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Mon, 1 Apr 2019 20:33:53 -0700 Subject: [PATCH 1/2] Removed Panel-specific transpose items --- pandas/compat/numpy/function.py | 17 ------------- pandas/core/generic.py | 44 --------------------------------- pandas/core/panel.py | 19 -------------- 3 files changed, 80 deletions(-) diff --git a/pandas/compat/numpy/function.py b/pandas/compat/numpy/function.py index 0ce0f1da483f1..112c0289a02e0 100644 --- a/pandas/compat/numpy/function.py +++ b/pandas/compat/numpy/function.py @@ -282,23 +282,6 @@ def validate_take_with_convert(convert, args, kwargs): method='both', max_fname_arg_count=0) -def validate_transpose_for_generic(inst, kwargs): - try: - validate_transpose(tuple(), kwargs) - except ValueError as e: - klass = type(inst).__name__ - msg = str(e) - - # the Panel class actual relies on the 'axes' parameter if called - # via the 'numpy' library, so let's make sure the error is specific - # about saying that the parameter is not supported for particular - # implementations of 'transpose' - if "the 'axes' parameter is not supported" in msg: - msg += " for {klass} instances".format(klass=klass) - - raise ValueError(msg) - - def validate_window_func(name, args, kwargs): numpy_args = ('axis', 'dtype', 'out') msg = ("numpy operations are not " diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2dcc7d3b0e60c..cb8fa2119ac79 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -651,50 +651,6 @@ def _set_axis(self, axis, labels): self._data.set_axis(axis, labels) self._clear_item_cache() - def transpose(self, *args, **kwargs): - """ - Permute the dimensions of the %(klass)s - - Parameters - ---------- - args : %(args_transpose)s - copy : boolean, default False - Make a copy of the underlying data. Mixed-dtype data will - always result in a copy - **kwargs - Additional keyword arguments will be passed to the function. - - Returns - ------- - y : same as input - - Examples - -------- - >>> p.transpose(2, 0, 1) - >>> p.transpose(2, 0, 1, copy=True) - """ - - # construct the args - axes, kwargs = self._construct_axes_from_arguments(args, kwargs, - require_all=True) - axes_names = tuple(self._get_axis_name(axes[a]) - for a in self._AXIS_ORDERS) - axes_numbers = tuple(self._get_axis_number(axes[a]) - for a in self._AXIS_ORDERS) - - # we must have unique axes - if len(axes) != len(set(axes)): - raise ValueError('Must specify %s unique axes' % self._AXIS_LEN) - - new_axes = self._construct_axes_dict_from(self, [self._get_axis(x) - for x in axes_names]) - new_values = self.values.transpose(axes_numbers) - if kwargs.pop('copy', None) or (len(args) and args[-1]): - new_values = new_values.copy() - - nv.validate_transpose_for_generic(self, kwargs) - return self._constructor(new_values, **new_axes).__finalize__(self) - def swapaxes(self, axis1, axis2, copy=True): """ Interchange axes and swap values axes appropriately. diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 542b1075313bf..9279a056b4f11 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1264,25 +1264,6 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, copy=copy, limit=limit, fill_value=fill_value) - @Substitution(**_shared_doc_kwargs) - @Appender(NDFrame.transpose.__doc__) - def transpose(self, *args, **kwargs): - # check if a list of axes was passed in instead as a - # single *args element - if (len(args) == 1 and hasattr(args[0], '__iter__') and - not is_string_like(args[0])): - axes = args[0] - else: - axes = args - - if 'axes' in kwargs and axes: - raise TypeError("transpose() got multiple values for " - "keyword argument 'axes'") - elif not axes: - axes = kwargs.pop('axes', ()) - - return super(Panel, self).transpose(*axes, **kwargs) - @Substitution(**_shared_doc_kwargs) @Appender(NDFrame.fillna.__doc__) def fillna(self, value=None, method=None, axis=None, inplace=False, From 8da647ec6aa2cb5df72cfba2c87fbc245874cd8d Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Mon, 1 Apr 2019 21:38:52 -0700 Subject: [PATCH 2/2] Hacked together new signatures --- pandas/core/frame.py | 8 ++------ pandas/core/generic.py | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6ca42130e86e5..0c9322b0fa274 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2545,7 +2545,7 @@ def memory_usage(self, index=True, deep=False): index=['Index']).append(result) return result - def transpose(self, *args, **kwargs): + def transpose(self, copy=False): """ Transpose index and columns. @@ -2558,9 +2558,6 @@ def transpose(self, *args, **kwargs): copy : bool, default False If True, the underlying data is copied. Otherwise (default), no copy is made if possible. - *args, **kwargs - Additional keywords have no effect but might be accepted for - compatibility with numpy. Returns ------- @@ -2640,8 +2637,7 @@ def transpose(self, *args, **kwargs): 1 object dtype: object """ - nv.validate_transpose(args, dict()) - return super(DataFrame, self).transpose(1, 0, **kwargs) + return super().transpose(copy) T = property(transpose) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index cb8fa2119ac79..e3de63f52fef8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -306,9 +306,8 @@ def _construct_axes_from_arguments( supplied; useful to distinguish when a user explicitly passes None in scenarios where None has special meaning. """ - - # construct the args args = list(args) + # construct the args for a in self._AXIS_ORDERS: # if we have an alias for this axis @@ -651,6 +650,41 @@ def _set_axis(self, axis, labels): self._data.set_axis(axis, labels) self._clear_item_cache() + def transpose(self, copy, valid_axes=[1, 0]): + """ + Permute the dimensions of the %(klass)s + + Parameters + ---------- + copy : boolean, default False + Make a copy of the underlying data. Mixed-dtype data will + always result in a copy + valid_axes : list of str or int + Names / aliases for axes to be transposed. + + Returns + ------- + y : same as input + """ + # construct the args + axes, kwargs = self._construct_axes_from_arguments(valid_axes, dict()) + axes_names = tuple(self._get_axis_name(axes[a]) + for a in self._AXIS_ORDERS) + axes_numbers = tuple(self._get_axis_number(axes[a]) + for a in self._AXIS_ORDERS) + + # we must have unique axes + if len(axes) != len(set(axes)): + raise ValueError('Must specify %s unique axes' % self._AXIS_LEN) + + new_axes = self._construct_axes_dict_from(self, [self._get_axis(x) + for x in axes_names]) + new_values = self.values.transpose(axes_numbers) + if copy: + new_values = new_values.copy() + + return self._constructor(new_values, **new_axes).__finalize__(self) + def swapaxes(self, axis1, axis2, copy=True): """ Interchange axes and swap values axes appropriately.