From f26f574744f86272c48fba17f0a8cd0da7fd9907 Mon Sep 17 00:00:00 2001 From: makbigc Date: Wed, 29 May 2019 15:24:44 +0800 Subject: [PATCH 1/5] Fixturize tests/frame/test_quantile.py --- pandas/tests/frame/test_quantile.py | 35 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index a5771839e0997..b18c79123a375 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -3,24 +3,23 @@ import pandas as pd from pandas import DataFrame, Series, Timestamp -from pandas.tests.frame.common import TestData import pandas.util.testing as tm from pandas.util.testing import assert_frame_equal, assert_series_equal -class TestDataFrameQuantile(TestData): +class TestDataFrameQuantile(): - def test_quantile(self): + def test_quantile(self, datetime_frame): from numpy import percentile - q = self.tsframe.quantile(0.1, axis=0) - assert q['A'] == percentile(self.tsframe['A'], 10) - tm.assert_index_equal(q.index, self.tsframe.columns) + q = datetime_frame.quantile(0.1, axis=0) + assert q['A'] == percentile(datetime_frame['A'], 10) + tm.assert_index_equal(q.index, datetime_frame.columns) - q = self.tsframe.quantile(0.9, axis=1) + q = datetime_frame.quantile(0.9, axis=1) assert (q['2000-01-17'] == - percentile(self.tsframe.loc['2000-01-17'], 90)) - tm.assert_index_equal(q.index, self.tsframe.index) + percentile(datetime_frame.loc['2000-01-17'], 90)) + tm.assert_index_equal(q.index, datetime_frame.index) # test degenerate case q = DataFrame({'x': [], 'y': []}).quantile(0.1, axis=0) @@ -97,19 +96,19 @@ def test_quantile_axis_parameter(self): with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis="column") - def test_quantile_interpolation(self): + def test_quantile_interpolation(self, datetime_frame, int_frame): # see gh-10174 from numpy import percentile # interpolation = linear (default case) - q = self.tsframe.quantile(0.1, axis=0, interpolation='linear') - assert q['A'] == percentile(self.tsframe['A'], 10) - q = self.intframe.quantile(0.1) - assert q['A'] == percentile(self.intframe['A'], 10) + q = datetime_frame.quantile(0.1, axis=0, interpolation='linear') + assert q['A'] == percentile(datetime_frame['A'], 10) + q = int_frame.quantile(0.1) + assert q['A'] == percentile(int_frame['A'], 10) # test with and without interpolation keyword - q1 = self.intframe.quantile(0.1) - assert q1['A'] == np.percentile(self.intframe['A'], 10) + q1 = int_frame.quantile(0.1) + assert q1['A'] == np.percentile(int_frame['A'], 10) tm.assert_series_equal(q, q1) # interpolation method other than default linear @@ -214,11 +213,11 @@ def test_quantile_datetime(self): # result = df[['a', 'c']].quantile(.5) # result = df[['a', 'c']].quantile([.5]) - def test_quantile_invalid(self): + def test_quantile_invalid(self, datetime_frame): msg = 'percentiles should all be in the interval \\[0, 1\\]' for invalid in [-1, 2, [0.5, -1], [0.5, 2]]: with pytest.raises(ValueError, match=msg): - self.tsframe.quantile(invalid) + datetime_frame.quantile(invalid) def test_quantile_box(self): df = DataFrame({'A': [pd.Timestamp('2011-01-01'), From 93478949fe22f070adb615c738f93909a248bbb8 Mon Sep 17 00:00:00 2001 From: makbigc Date: Wed, 29 May 2019 16:15:52 +0800 Subject: [PATCH 2/5] Remove unwanted pattern --- pandas/tests/frame/test_quantile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index b18c79123a375..da94227af81dd 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -7,7 +7,7 @@ from pandas.util.testing import assert_frame_equal, assert_series_equal -class TestDataFrameQuantile(): +class TestDataFrameQuantile: def test_quantile(self, datetime_frame): from numpy import percentile From f67da860f311a2b0c3219b3c37df8b971c8bcc5b Mon Sep 17 00:00:00 2001 From: makbigc Date: Thu, 30 May 2019 22:35:07 +0800 Subject: [PATCH 3/5] Separate tests --- pandas/tests/frame/test_quantile.py | 48 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index da94227af81dd..637fff80e9645 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -12,14 +12,15 @@ class TestDataFrameQuantile: def test_quantile(self, datetime_frame): from numpy import percentile - q = datetime_frame.quantile(0.1, axis=0) - assert q['A'] == percentile(datetime_frame['A'], 10) - tm.assert_index_equal(q.index, datetime_frame.columns) + df = datetime_frame + q = df.quantile(0.1, axis=0) + assert q['A'] == percentile(df['A'], 10) + tm.assert_index_equal(q.index, df.columns) - q = datetime_frame.quantile(0.9, axis=1) + q = df.quantile(0.9, axis=1) assert (q['2000-01-17'] == - percentile(datetime_frame.loc['2000-01-17'], 90)) - tm.assert_index_equal(q.index, datetime_frame.index) + percentile(df.loc['2000-01-17'], 90)) + tm.assert_index_equal(q.index, df.index) # test degenerate case q = DataFrame({'x': [], 'y': []}).quantile(0.1, axis=0) @@ -96,20 +97,8 @@ def test_quantile_axis_parameter(self): with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis="column") - def test_quantile_interpolation(self, datetime_frame, int_frame): + def test_quantile_interpolation(self): # see gh-10174 - from numpy import percentile - - # interpolation = linear (default case) - q = datetime_frame.quantile(0.1, axis=0, interpolation='linear') - assert q['A'] == percentile(datetime_frame['A'], 10) - q = int_frame.quantile(0.1) - assert q['A'] == percentile(int_frame['A'], 10) - - # test with and without interpolation keyword - q1 = int_frame.quantile(0.1) - assert q1['A'] == np.percentile(int_frame['A'], 10) - tm.assert_series_equal(q, q1) # interpolation method other than default linear df = DataFrame({"A": [1, 2, 3], "B": [2, 3, 4]}, index=[1, 2, 3]) @@ -154,6 +143,27 @@ def test_quantile_interpolation(self, datetime_frame, int_frame): index=[.25, .5], columns=['a', 'b', 'c']) assert_frame_equal(result, expected) + def test_quantile_interpolation_datetime(self, datetime_frame): + # see gh-10174 + + # interpolation = linear (default case) + df = datetime_frame + q = df.quantile(0.1, axis=0, interpolation='linear') + assert q['A'] == np.percentile(df['A'], 10) + + def test_quantile_interpolation_int(self, int_frame): + # see gh-10174 + + df = int_frame + # interpolation = linear (default case) + q = df.quantile(0.1) + assert q['A'] == np.percentile(df['A'], 10) + + # test with and without interpolation keyword + q1 = df.quantile(0.1) + assert q1['A'] == np.percentile(df['A'], 10) + tm.assert_series_equal(q, q1) + def test_quantile_multi(self): df = DataFrame([[1, 1, 1], [2, 2, 2], [3, 3, 3]], columns=['a', 'b', 'c']) From ecb0bbb2e26db8f09d5c9d24ce66474b64324a4c Mon Sep 17 00:00:00 2001 From: makbigc Date: Thu, 30 May 2019 23:53:37 +0800 Subject: [PATCH 4/5] Add TODO note --- pandas/tests/frame/test_quantile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index 637fff80e9645..aa2eecb00c1b9 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -159,6 +159,7 @@ def test_quantile_interpolation_int(self, int_frame): q = df.quantile(0.1) assert q['A'] == np.percentile(df['A'], 10) + # TODO: Remove the following, as q1 is not different from q # test with and without interpolation keyword q1 = df.quantile(0.1) assert q1['A'] == np.percentile(df['A'], 10) From ad6a5d88963700ad03df3862bae89abc3920916c Mon Sep 17 00:00:00 2001 From: makbigc Date: Fri, 31 May 2019 16:34:35 +0800 Subject: [PATCH 5/5] Change TODO comment --- pandas/tests/frame/test_quantile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index aa2eecb00c1b9..9ccbd290923ba 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -159,8 +159,8 @@ def test_quantile_interpolation_int(self, int_frame): q = df.quantile(0.1) assert q['A'] == np.percentile(df['A'], 10) - # TODO: Remove the following, as q1 is not different from q # test with and without interpolation keyword + # TODO: q1 is not different from q q1 = df.quantile(0.1) assert q1['A'] == np.percentile(df['A'], 10) tm.assert_series_equal(q, q1)