Skip to content

ENH + BUG: insert of new values for axis parameter on pandas.DataFrame.quantile #9544

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

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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`)
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number here


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'])
Expand Down