Skip to content

TST/CLN: Fixturize tests/frame/test_quantile.py #26556

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

Merged
merged 5 commits into from
Jun 1, 2019
Merged
Changes from 4 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
56 changes: 33 additions & 23 deletions pandas/tests/frame/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

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)
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 = self.tsframe.quantile(0.9, axis=1)
q = df.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(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)
Expand Down Expand Up @@ -99,18 +99,6 @@ def test_quantile_axis_parameter(self):

def test_quantile_interpolation(self):
# 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)

# test with and without interpolation keyword
q1 = self.intframe.quantile(0.1)
assert q1['A'] == np.percentile(self.intframe['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])
Expand Down Expand Up @@ -155,6 +143,28 @@ def test_quantile_interpolation(self):
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)

# 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)
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'])
Expand Down Expand Up @@ -214,11 +224,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'),
Expand Down