Skip to content

TST/REF: collect tests by method from generic #37421

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 4 commits into from
Oct 27, 2020
Merged
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
10 changes: 9 additions & 1 deletion pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import numpy as np
import pytest

from pandas import DataFrame, Index, MultiIndex
from pandas import DataFrame, Index, MultiIndex, Series
import pandas._testing as tm


class TestRename:
@pytest.mark.parametrize("klass", [Series, DataFrame])
def test_rename_mi(self, klass):
obj = klass(
[11, 21, 31],
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
)
obj.rename(str.lower)

def test_rename(self, float_frame):
mapping = {"A": "a", "B": "b", "C": "c", "D": "d"}

Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/frame/methods/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import numpy as np
import pytest

from pandas.compat.numpy import np_version_under1p17

from pandas import DataFrame
import pandas._testing as tm
import pandas.core.common as com


class TestSample:
@pytest.mark.parametrize(
"func_str,arg",
[
("np.array", [2, 3, 1, 0]),
pytest.param(
"np.random.MT19937",
3,
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
),
pytest.param(
"np.random.PCG64",
11,
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
),
],
)
def test_sample_random_state(self, func_str, arg):
# GH#32503
df = DataFrame({"col1": range(10, 20), "col2": range(20, 30)})
result = df.sample(n=3, random_state=eval(func_str)(arg))
expected = df.sample(n=3, random_state=com.random_state(eval(func_str)(arg)))
tm.assert_frame_equal(result, expected)

def test_sample_upsampling_without_replacement(self):
# GH#27451

df = DataFrame({"A": list("abc")})
msg = (
"Replace has to be set to `True` when "
"upsampling the population `frac` > 1."
)
with pytest.raises(ValueError, match=msg):
df.sample(frac=2, replace=False)

def test_sample_is_copy(self):
# GH#27357, GH#30784: ensure the result of sample is an actual copy and
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
df = DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
df2 = df.sample(3)

with tm.assert_produces_warning(None):
df2["d"] = 1
38 changes: 38 additions & 0 deletions pandas/tests/generic/methods/test_pipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from pandas import DataFrame, Series
import pandas._testing as tm


class TestPipe:
@pytest.mark.parametrize("klass", [Series, DataFrame])
def test_pipe(self, klass):
obj = DataFrame({"A": [1, 2, 3]})
expected = DataFrame({"A": [1, 4, 9]})
if klass is Series:
obj = obj["A"]
expected = expected["A"]

f = lambda x, y: x ** y
result = obj.pipe(f, 2)
tm.assert_equal(result, expected)

@pytest.mark.parametrize("klass", [Series, DataFrame])
def test_pipe_tuple(self, klass):
obj = DataFrame({"A": [1, 2, 3]})
if klass is Series:
obj = obj["A"]

f = lambda x, y: y
result = obj.pipe((f, "y"), 0)
tm.assert_equal(result, obj)

@pytest.mark.parametrize("klass", [Series, DataFrame])
def test_pipe_tuple_error(self, klass):
obj = DataFrame({"A": [1, 2, 3]})
if klass is Series:
obj = obj["A"]

f = lambda x, y: y
with pytest.raises(ValueError):
obj.pipe((f, "y"), x=1, y=0)
12 changes: 3 additions & 9 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class TestDataFrame(Generic):
_typ = DataFrame
_comparator = lambda self, x, y: tm.assert_frame_equal(x, y)

def test_rename_mi(self):
df = DataFrame(
[11, 21, 31],
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
)
df.rename(str.lower)

@pytest.mark.parametrize("func", ["_set_axis_name", "rename_axis"])
def test_set_axis_name(self, func):
df = DataFrame([[1, 2], [3, 4]])
Expand Down Expand Up @@ -76,8 +69,7 @@ def test_get_numeric_data_preserve_dtype(self):
expected = DataFrame(index=[0, 1, 2], dtype=object)
self._compare(result, expected)

def test_metadata_propagation_indiv(self):

def test_metadata_propagation_indiv_groupby(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm thought we had a specific module for metadata tests

Copy link
Member Author

Choose a reason for hiding this comment

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

@TomAugspurger is there a better home for these?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the delay. We have some in pandas/tests/generic/test_finalize.py.

# groupby
df = DataFrame(
{
Expand All @@ -90,6 +82,7 @@ def test_metadata_propagation_indiv(self):
result = df.groupby("A").sum()
self.check_metadata(df, result)

def test_metadata_propagation_indiv_resample(self):
# resample
df = DataFrame(
np.random.randn(1000, 2),
Expand All @@ -98,6 +91,7 @@ def test_metadata_propagation_indiv(self):
result = df.resample("1T")
self.check_metadata(df, result)

def test_metadata_propagation_indiv(self):
# merging with override
# GH 6923
_metadata = DataFrame._metadata
Expand Down
76 changes: 1 addition & 75 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import numpy as np
import pytest

from pandas.compat.numpy import np_version_under1p17

from pandas.core.dtypes.common import is_scalar

import pandas as pd
from pandas import DataFrame, Series, date_range
import pandas._testing as tm
import pandas.core.common as com

# ----------------------------------------------------------------------
# Generic types test cases
Expand Down Expand Up @@ -404,26 +401,6 @@ def test_sample(self):
weights_with_None[5] = 0.5
self._compare(o.sample(n=1, axis=0, weights=weights_with_None), o.iloc[5:6])

def test_sample_upsampling_without_replacement(self):
# GH27451

df = DataFrame({"A": list("abc")})
msg = (
"Replace has to be set to `True` when "
"upsampling the population `frac` > 1."
)
with pytest.raises(ValueError, match=msg):
df.sample(frac=2, replace=False)

def test_sample_is_copy(self):
# GH-27357, GH-30784: ensure the result of sample is an actual copy and
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
df = DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
df2 = df.sample(3)

with tm.assert_produces_warning(None):
df2["d"] = 1

def test_size_compat(self):
# GH8846
# size property should be defined
Expand Down Expand Up @@ -534,7 +511,7 @@ def test_pct_change(self, periods, fill_method, limit, exp):
class TestNDFrame:
# tests that don't fit elsewhere

def test_sample(sel):
Copy link
Contributor

Choose a reason for hiding this comment

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

wow, wonder how this worked

def test_sample(self):
# Fixes issue: 2419
# additional specific object based tests

Expand Down Expand Up @@ -645,29 +622,6 @@ def test_sample(sel):
with pytest.raises(ValueError):
df.sample(1, weights=s4)

@pytest.mark.parametrize(
"func_str,arg",
[
("np.array", [2, 3, 1, 0]),
pytest.param(
"np.random.MT19937",
3,
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
),
pytest.param(
"np.random.PCG64",
11,
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
),
],
)
def test_sample_random_state(self, func_str, arg):
# GH32503
df = DataFrame({"col1": range(10, 20), "col2": range(20, 30)})
result = df.sample(n=3, random_state=eval(func_str)(arg))
expected = df.sample(n=3, random_state=com.random_state(eval(func_str)(arg)))
tm.assert_frame_equal(result, expected)

def test_squeeze(self):
# noop
for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]:
Expand Down Expand Up @@ -837,34 +791,6 @@ def test_equals(self):
df2 = df1.set_index(["floats"], append=True)
assert df3.equals(df2)

def test_pipe(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: x ** y
result = df.pipe(f, 2)
expected = DataFrame({"A": [1, 4, 9]})
tm.assert_frame_equal(result, expected)

result = df.A.pipe(f, 2)
tm.assert_series_equal(result, expected.A)

def test_pipe_tuple(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: y
result = df.pipe((f, "y"), 0)
tm.assert_frame_equal(result, df)

result = df.A.pipe((f, "y"), 0)
tm.assert_series_equal(result, df.A)

def test_pipe_tuple_error(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: y
with pytest.raises(ValueError):
df.pipe((f, "y"), x=1, y=0)

with pytest.raises(ValueError):
df.A.pipe((f, "y"), x=1, y=0)

@pytest.mark.parametrize("box", [pd.Series, pd.DataFrame])
def test_axis_classmethods(self, box):
obj = box(dtype=object)
Expand Down
52 changes: 12 additions & 40 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ class TestSeries(Generic):
_typ = Series
_comparator = lambda self, x, y: tm.assert_series_equal(x, y)

def test_rename_mi(self):
s = Series(
[11, 21, 31],
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
)
s.rename(str.lower)

@pytest.mark.parametrize("func", ["rename_axis", "_set_axis_name"])
def test_set_axis_name_mi(self, func):
s = Series(
Expand Down Expand Up @@ -104,17 +97,7 @@ def test_nonzero_single_element(self):
with pytest.raises(ValueError, match=msg):
s.bool()

def test_metadata_propagation_indiv(self):
# check that the metadata matches up on the resulting ops

o = Series(range(3), range(3))
o.name = "foo"
o2 = Series(range(3), range(3))
o2.name = "bar"

result = o.T
self.check_metadata(o, result)

def test_metadata_propagation_indiv_resample(self):
# resample
ts = Series(
np.random.rand(1000),
Expand All @@ -130,6 +113,17 @@ def test_metadata_propagation_indiv(self):
result = ts.resample("1T").apply(lambda x: x.sum())
self.check_metadata(ts, result)

def test_metadata_propagation_indiv(self):
# check that the metadata matches up on the resulting ops

o = Series(range(3), range(3))
o.name = "foo"
o2 = Series(range(3), range(3))
o2.name = "bar"

result = o.T
self.check_metadata(o, result)

_metadata = Series._metadata
_finalize = Series.__finalize__
Series._metadata = ["name", "filename"]
Expand Down Expand Up @@ -157,25 +151,3 @@ def finalize(self, other, method=None, **kwargs):
# reset
Series._metadata = _metadata
Series.__finalize__ = _finalize # FIXME: use monkeypatch


class TestSeries2:
# Separating off because it doesnt rely on parent class
@pytest.mark.parametrize(
"s",
[
Series([np.arange(5)]),
pd.date_range("1/1/2011", periods=24, freq="H"),
Series(range(5), index=pd.date_range("2017", periods=5)),
],
)
@pytest.mark.parametrize("shift_size", [0, 1, 2])
def test_shift_always_copy(self, s, shift_size):
# GH22397
assert s.shift(shift_size) is not s

@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
def test_datetime_shift_always_copy(self, move_by_freq):
# GH22397
s = Series(range(5), index=pd.date_range("2017", periods=5))
assert s.shift(freq=move_by_freq) is not s
19 changes: 19 additions & 0 deletions pandas/tests/series/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@


class TestShift:
@pytest.mark.parametrize(
"ser",
[
Series([np.arange(5)]),
date_range("1/1/2011", periods=24, freq="H"),
Series(range(5), index=date_range("2017", periods=5)),
],
)
@pytest.mark.parametrize("shift_size", [0, 1, 2])
def test_shift_always_copy(self, ser, shift_size):
# GH22397
assert ser.shift(shift_size) is not ser

@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
def test_datetime_shift_always_copy(self, move_by_freq):
# GH#22397
ser = Series(range(5), index=date_range("2017", periods=5))
assert ser.shift(freq=move_by_freq) is not ser

def test_shift(self, datetime_series):
shifted = datetime_series.shift(1)
unshifted = shifted.shift(-1)
Expand Down