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`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ab6f11a4b8d5b..2b434c98d8482 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4477,8 +4477,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 ------- @@ -4524,6 +4525,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 f74cb07557342..1f72beddc968a 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -12066,6 +12066,31 @@ def test_quantile(self): expected = Series([3., 4.], index=[0, 1]) 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]) + + 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'])