From b43d513e8a60cc317d35d2177e1eeb12edff8b08 Mon Sep 17 00:00:00 2001 From: TomAugspurger Date: Mon, 2 Jun 2014 09:50:29 -0500 Subject: [PATCH] BUG: quantile ignores axis kwarg --- doc/source/v0.14.1.txt | 1 + pandas/core/frame.py | 2 ++ pandas/tests/test_frame.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index 20620f15944f0..df2e86ed34d7e 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -111,3 +111,4 @@ Bug Fixes - Bug in ``CustomBusinessDay.apply`` raiases ``NameError`` when ``np.datetime64`` object is passed (:issue:`7196`) - Bug in ``MultiIndex.append``, ``concat`` and ``pivot_table`` don't preserve timezone (:issue:`6606`) - Bug all ``StringMethods`` now work on empty Series (:issue:`7242`) +- Bug in ``quantile`` ignoring the axis keyword argument (:issue`7306`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fb39aced6ec1d..72ecddc29646b 100755 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4181,6 +4181,8 @@ def f(arr, per): return _quantile(values, per) data = self._get_numeric_data() if numeric_only else self + if axis == 1: + data = data.T # need to know which cols are timestamp going in so that we can # map timestamp over them after getting the quantile. diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 358d9d82403f6..794563a219840 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -11103,6 +11103,26 @@ def test_quantile(self): xp = df.median() assert_series_equal(rs, xp) + # axis + df = DataFrame({"A": [1, 2, 3], "B": [2, 3, 4]}, index=[1, 2, 3]) + 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, .75], axis=1) + expected = DataFrame({1: [1.5, 1.75], 2: [2.5, 2.75], + 3: [3.5, 3.75]}, index=["0.5", "0.75"]) + assert_frame_equal(result, expected) + + # We may want to break API in the future to change this + # so that we exclude non-numeric along the same axis + # See GH #7312 + df = DataFrame([[1, 2, 3], + ['a', 'b', 4]]) + result = df.quantile(.5, axis=1) + expected = Series([3., 4.], index=[0, 1]) + assert_series_equal(result, expected) + def test_quantile_multi(self): df = DataFrame([[1, 1, 1], [2, 2, 2], [3, 3, 3]], columns=['a', 'b', 'c']) @@ -11141,6 +11161,20 @@ def test_quantile_datetime(self): index=[.5], columns=['a', 'b']) assert_frame_equal(result, expected) + # axis = 1 + df['c'] = pd.to_datetime(['2011', '2012']) + result = df[['a', 'c']].quantile(.5, axis=1, numeric_only=False) + expected = Series([Timestamp('2010-07-02 12:00:00'), + Timestamp('2011-07-02 12:00:00')], + index=[0, 1]) + assert_series_equal(result, expected) + + result = df[['a', 'c']].quantile([.5], axis=1, numeric_only=False) + expected = DataFrame([[Timestamp('2010-07-02 12:00:00'), + Timestamp('2011-07-02 12:00:00')]], + index=[0.5], columns=[0, 1]) + assert_frame_equal(result, expected) + def test_cumsum(self): self.tsframe.ix[5:10, 0] = nan self.tsframe.ix[10:15, 1] = nan