From 28c68a4fd3baa5607d0e57bbf323f5d13b5743ba Mon Sep 17 00:00:00 2001 From: Francesco Brundu Date: Tue, 24 Feb 2015 21:56:15 +0100 Subject: [PATCH 1/3] ENH: insert of new values for axis parameter on pandas.DataFrame.quantile() --- pandas/core/frame.py | 8 ++++++-- pandas/tests/test_frame.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d64353db8cda6..bf04b21d1c505 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4363,8 +4363,9 @@ def quantile(self, q=0.5, axis=0, numeric_only=True): ---------- q : float or array-like, default 0.5 (50% quantile) 0 <= q <= 1, the quantile(s) to compute - axis : {0, 1} - 0 for row-wise, 1 for column-wise + axis : {0, 1, 'index', 'columns'} (default 0) + 0 or 'index' for row-wise, 1 or 'columns' for column-wise + Returns ------- @@ -4410,6 +4411,9 @@ def f(arr, per): return _quantile(values, per) data = self._get_numeric_data() if numeric_only else self + + axis = self._get_axis_number(axis) + if axis == 1: data = data.T diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 291bb8a88c857..447840e6875dd 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -11786,6 +11786,30 @@ def test_quantile(self): expected = Series([3., 4.], index=[0, 1]) assert_series_equal(result, expected) + def test_quantile_axis_parameter(self): + from numpy import percentile + + df = DataFrame({"A": [1, 2, 3], "B": [2, 3, 4]}, index=[1, 2, 3]) + + result = df.quantile(.5, axis=0) + + expected = Series([2., 3.], index=["A", "B"]) + assert_series_equal(result, expected) + + expected = df.quantile(.5, axis="index") + assert_series_equal(result, expected) + + result = df.quantile(.5, axis=1) + + expected = Series([1.5, 2.5, 3.5], index=[1, 2, 3]) + assert_series_equal(result, expected) + + result = df.quantile(.5, axis="columns") + assert_series_equal(result, expected) + + assertRaises(ValueError, df.quantile, 0.1, axis=-1) + assertRaises(ValueError, df.quantile, 0.1, axis="column") + def test_quantile_multi(self): df = DataFrame([[1, 1, 1], [2, 2, 2], [3, 3, 3]], columns=['a', 'b', 'c']) From ae3db2e274320b526d288421743956129c0e914f Mon Sep 17 00:00:00 2001 From: Francesco Brundu Date: Fri, 5 Jun 2015 11:07:56 +0200 Subject: [PATCH 2/3] Update test_frame.py --- pandas/tests/test_frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 447840e6875dd..9e1706cc7d953 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -11787,6 +11787,7 @@ def test_quantile(self): assert_series_equal(result, expected) def test_quantile_axis_parameter(self): + # GH 9543/9544 from numpy import percentile df = DataFrame({"A": [1, 2, 3], "B": [2, 3, 4]}, index=[1, 2, 3]) From 6924e65fdc713151aa9fe23880522805e0df53cd Mon Sep 17 00:00:00 2001 From: Francesco Brundu Date: Fri, 5 Jun 2015 12:06:11 +0200 Subject: [PATCH 3/3] Doc for PR 9544 --- doc/source/whatsnew/v0.16.2.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.16.2.txt b/doc/source/whatsnew/v0.16.2.txt index 73ae5351ca374..b4f696ee27cbf 100644 --- a/doc/source/whatsnew/v0.16.2.txt +++ b/doc/source/whatsnew/v0.16.2.txt @@ -40,6 +40,7 @@ Other API Changes ^^^^^^^^^^^^^^^^^ - ``Holiday`` now raises ``NotImplementedError`` if both ``offset`` and ``observance`` are used in constructor instead of returning an incorrect result (:issue:`10217`). +- ``axis`` parameter of method ``DataFrame.quantile`` now accepts also ``index`` and``column``. (:issue:`9543`) .. _whatsnew_0162.performance: @@ -57,7 +58,7 @@ Bug Fixes - Bug where read_hdf store.select modifies the passed columns list when multi-indexed (:issue:`7212`) - Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`) - +- Bug in ``DataFrame.quantile``: no check on the value inserted for the ``axis`` parameter (if 'foo' was inserted, no ``ValueError()`` was raised) (:issue:`9543`) - Bug in groupby.apply aggregation for Categorical not preserving categories (:issue:`10138`) - Bug in ``mean()`` where integer dtypes can overflow (:issue:`10172`) - Bug where Panel.from_dict does not set dtype when specified (:issue:`10058`)