Skip to content

Commit efb068f

Browse files
authored
TST/REF: collect tests by method from generic (#37421)
1 parent 0078120 commit efb068f

File tree

7 files changed

+135
-125
lines changed

7 files changed

+135
-125
lines changed

pandas/tests/frame/methods/test_rename.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
import numpy as np
44
import pytest
55

6-
from pandas import DataFrame, Index, MultiIndex
6+
from pandas import DataFrame, Index, MultiIndex, Series
77
import pandas._testing as tm
88

99

1010
class TestRename:
11+
@pytest.mark.parametrize("klass", [Series, DataFrame])
12+
def test_rename_mi(self, klass):
13+
obj = klass(
14+
[11, 21, 31],
15+
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
16+
)
17+
obj.rename(str.lower)
18+
1119
def test_rename(self, float_frame):
1220
mapping = {"A": "a", "B": "b", "C": "c", "D": "d"}
1321

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas.compat.numpy import np_version_under1p17
5+
6+
from pandas import DataFrame
7+
import pandas._testing as tm
8+
import pandas.core.common as com
9+
10+
11+
class TestSample:
12+
@pytest.mark.parametrize(
13+
"func_str,arg",
14+
[
15+
("np.array", [2, 3, 1, 0]),
16+
pytest.param(
17+
"np.random.MT19937",
18+
3,
19+
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
20+
),
21+
pytest.param(
22+
"np.random.PCG64",
23+
11,
24+
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
25+
),
26+
],
27+
)
28+
def test_sample_random_state(self, func_str, arg):
29+
# GH#32503
30+
df = DataFrame({"col1": range(10, 20), "col2": range(20, 30)})
31+
result = df.sample(n=3, random_state=eval(func_str)(arg))
32+
expected = df.sample(n=3, random_state=com.random_state(eval(func_str)(arg)))
33+
tm.assert_frame_equal(result, expected)
34+
35+
def test_sample_upsampling_without_replacement(self):
36+
# GH#27451
37+
38+
df = DataFrame({"A": list("abc")})
39+
msg = (
40+
"Replace has to be set to `True` when "
41+
"upsampling the population `frac` > 1."
42+
)
43+
with pytest.raises(ValueError, match=msg):
44+
df.sample(frac=2, replace=False)
45+
46+
def test_sample_is_copy(self):
47+
# GH#27357, GH#30784: ensure the result of sample is an actual copy and
48+
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
49+
df = DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
50+
df2 = df.sample(3)
51+
52+
with tm.assert_produces_warning(None):
53+
df2["d"] = 1
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from pandas import DataFrame, Series
4+
import pandas._testing as tm
5+
6+
7+
class TestPipe:
8+
@pytest.mark.parametrize("klass", [Series, DataFrame])
9+
def test_pipe(self, klass):
10+
obj = DataFrame({"A": [1, 2, 3]})
11+
expected = DataFrame({"A": [1, 4, 9]})
12+
if klass is Series:
13+
obj = obj["A"]
14+
expected = expected["A"]
15+
16+
f = lambda x, y: x ** y
17+
result = obj.pipe(f, 2)
18+
tm.assert_equal(result, expected)
19+
20+
@pytest.mark.parametrize("klass", [Series, DataFrame])
21+
def test_pipe_tuple(self, klass):
22+
obj = DataFrame({"A": [1, 2, 3]})
23+
if klass is Series:
24+
obj = obj["A"]
25+
26+
f = lambda x, y: y
27+
result = obj.pipe((f, "y"), 0)
28+
tm.assert_equal(result, obj)
29+
30+
@pytest.mark.parametrize("klass", [Series, DataFrame])
31+
def test_pipe_tuple_error(self, klass):
32+
obj = DataFrame({"A": [1, 2, 3]})
33+
if klass is Series:
34+
obj = obj["A"]
35+
36+
f = lambda x, y: y
37+
with pytest.raises(ValueError):
38+
obj.pipe((f, "y"), x=1, y=0)

pandas/tests/generic/test_frame.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class TestDataFrame(Generic):
1515
_typ = DataFrame
1616
_comparator = lambda self, x, y: tm.assert_frame_equal(x, y)
1717

18-
def test_rename_mi(self):
19-
df = DataFrame(
20-
[11, 21, 31],
21-
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
22-
)
23-
df.rename(str.lower)
24-
2518
@pytest.mark.parametrize("func", ["_set_axis_name", "rename_axis"])
2619
def test_set_axis_name(self, func):
2720
df = DataFrame([[1, 2], [3, 4]])
@@ -76,8 +69,7 @@ def test_get_numeric_data_preserve_dtype(self):
7669
expected = DataFrame(index=[0, 1, 2], dtype=object)
7770
self._compare(result, expected)
7871

79-
def test_metadata_propagation_indiv(self):
80-
72+
def test_metadata_propagation_indiv_groupby(self):
8173
# groupby
8274
df = DataFrame(
8375
{
@@ -90,6 +82,7 @@ def test_metadata_propagation_indiv(self):
9082
result = df.groupby("A").sum()
9183
self.check_metadata(df, result)
9284

85+
def test_metadata_propagation_indiv_resample(self):
9386
# resample
9487
df = DataFrame(
9588
np.random.randn(1000, 2),
@@ -98,6 +91,7 @@ def test_metadata_propagation_indiv(self):
9891
result = df.resample("1T")
9992
self.check_metadata(df, result)
10093

94+
def test_metadata_propagation_indiv(self):
10195
# merging with override
10296
# GH 6923
10397
_metadata = DataFrame._metadata

pandas/tests/generic/test_generic.py

+1-75
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.compat.numpy import np_version_under1p17
7-
86
from pandas.core.dtypes.common import is_scalar
97

108
import pandas as pd
119
from pandas import DataFrame, Series, date_range
1210
import pandas._testing as tm
13-
import pandas.core.common as com
1411

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

407-
def test_sample_upsampling_without_replacement(self):
408-
# GH27451
409-
410-
df = DataFrame({"A": list("abc")})
411-
msg = (
412-
"Replace has to be set to `True` when "
413-
"upsampling the population `frac` > 1."
414-
)
415-
with pytest.raises(ValueError, match=msg):
416-
df.sample(frac=2, replace=False)
417-
418-
def test_sample_is_copy(self):
419-
# GH-27357, GH-30784: ensure the result of sample is an actual copy and
420-
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
421-
df = DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
422-
df2 = df.sample(3)
423-
424-
with tm.assert_produces_warning(None):
425-
df2["d"] = 1
426-
427404
def test_size_compat(self):
428405
# GH8846
429406
# size property should be defined
@@ -534,7 +511,7 @@ def test_pct_change(self, periods, fill_method, limit, exp):
534511
class TestNDFrame:
535512
# tests that don't fit elsewhere
536513

537-
def test_sample(sel):
514+
def test_sample(self):
538515
# Fixes issue: 2419
539516
# additional specific object based tests
540517

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

648-
@pytest.mark.parametrize(
649-
"func_str,arg",
650-
[
651-
("np.array", [2, 3, 1, 0]),
652-
pytest.param(
653-
"np.random.MT19937",
654-
3,
655-
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
656-
),
657-
pytest.param(
658-
"np.random.PCG64",
659-
11,
660-
marks=pytest.mark.skipif(np_version_under1p17, reason="NumPy<1.17"),
661-
),
662-
],
663-
)
664-
def test_sample_random_state(self, func_str, arg):
665-
# GH32503
666-
df = DataFrame({"col1": range(10, 20), "col2": range(20, 30)})
667-
result = df.sample(n=3, random_state=eval(func_str)(arg))
668-
expected = df.sample(n=3, random_state=com.random_state(eval(func_str)(arg)))
669-
tm.assert_frame_equal(result, expected)
670-
671625
def test_squeeze(self):
672626
# noop
673627
for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]:
@@ -837,34 +791,6 @@ def test_equals(self):
837791
df2 = df1.set_index(["floats"], append=True)
838792
assert df3.equals(df2)
839793

840-
def test_pipe(self):
841-
df = DataFrame({"A": [1, 2, 3]})
842-
f = lambda x, y: x ** y
843-
result = df.pipe(f, 2)
844-
expected = DataFrame({"A": [1, 4, 9]})
845-
tm.assert_frame_equal(result, expected)
846-
847-
result = df.A.pipe(f, 2)
848-
tm.assert_series_equal(result, expected.A)
849-
850-
def test_pipe_tuple(self):
851-
df = DataFrame({"A": [1, 2, 3]})
852-
f = lambda x, y: y
853-
result = df.pipe((f, "y"), 0)
854-
tm.assert_frame_equal(result, df)
855-
856-
result = df.A.pipe((f, "y"), 0)
857-
tm.assert_series_equal(result, df.A)
858-
859-
def test_pipe_tuple_error(self):
860-
df = DataFrame({"A": [1, 2, 3]})
861-
f = lambda x, y: y
862-
with pytest.raises(ValueError):
863-
df.pipe((f, "y"), x=1, y=0)
864-
865-
with pytest.raises(ValueError):
866-
df.A.pipe((f, "y"), x=1, y=0)
867-
868794
@pytest.mark.parametrize("box", [pd.Series, pd.DataFrame])
869795
def test_axis_classmethods(self, box):
870796
obj = box(dtype=object)

pandas/tests/generic/test_series.py

+12-40
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ class TestSeries(Generic):
1414
_typ = Series
1515
_comparator = lambda self, x, y: tm.assert_series_equal(x, y)
1616

17-
def test_rename_mi(self):
18-
s = Series(
19-
[11, 21, 31],
20-
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
21-
)
22-
s.rename(str.lower)
23-
2417
@pytest.mark.parametrize("func", ["rename_axis", "_set_axis_name"])
2518
def test_set_axis_name_mi(self, func):
2619
s = Series(
@@ -104,17 +97,7 @@ def test_nonzero_single_element(self):
10497
with pytest.raises(ValueError, match=msg):
10598
s.bool()
10699

107-
def test_metadata_propagation_indiv(self):
108-
# check that the metadata matches up on the resulting ops
109-
110-
o = Series(range(3), range(3))
111-
o.name = "foo"
112-
o2 = Series(range(3), range(3))
113-
o2.name = "bar"
114-
115-
result = o.T
116-
self.check_metadata(o, result)
117-
100+
def test_metadata_propagation_indiv_resample(self):
118101
# resample
119102
ts = Series(
120103
np.random.rand(1000),
@@ -130,6 +113,17 @@ def test_metadata_propagation_indiv(self):
130113
result = ts.resample("1T").apply(lambda x: x.sum())
131114
self.check_metadata(ts, result)
132115

116+
def test_metadata_propagation_indiv(self):
117+
# check that the metadata matches up on the resulting ops
118+
119+
o = Series(range(3), range(3))
120+
o.name = "foo"
121+
o2 = Series(range(3), range(3))
122+
o2.name = "bar"
123+
124+
result = o.T
125+
self.check_metadata(o, result)
126+
133127
_metadata = Series._metadata
134128
_finalize = Series.__finalize__
135129
Series._metadata = ["name", "filename"]
@@ -157,25 +151,3 @@ def finalize(self, other, method=None, **kwargs):
157151
# reset
158152
Series._metadata = _metadata
159153
Series.__finalize__ = _finalize # FIXME: use monkeypatch
160-
161-
162-
class TestSeries2:
163-
# Separating off because it doesnt rely on parent class
164-
@pytest.mark.parametrize(
165-
"s",
166-
[
167-
Series([np.arange(5)]),
168-
pd.date_range("1/1/2011", periods=24, freq="H"),
169-
Series(range(5), index=pd.date_range("2017", periods=5)),
170-
],
171-
)
172-
@pytest.mark.parametrize("shift_size", [0, 1, 2])
173-
def test_shift_always_copy(self, s, shift_size):
174-
# GH22397
175-
assert s.shift(shift_size) is not s
176-
177-
@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
178-
def test_datetime_shift_always_copy(self, move_by_freq):
179-
# GH22397
180-
s = Series(range(5), index=pd.date_range("2017", periods=5))
181-
assert s.shift(freq=move_by_freq) is not s

pandas/tests/series/methods/test_shift.py

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@
1919

2020

2121
class TestShift:
22+
@pytest.mark.parametrize(
23+
"ser",
24+
[
25+
Series([np.arange(5)]),
26+
date_range("1/1/2011", periods=24, freq="H"),
27+
Series(range(5), index=date_range("2017", periods=5)),
28+
],
29+
)
30+
@pytest.mark.parametrize("shift_size", [0, 1, 2])
31+
def test_shift_always_copy(self, ser, shift_size):
32+
# GH22397
33+
assert ser.shift(shift_size) is not ser
34+
35+
@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
36+
def test_datetime_shift_always_copy(self, move_by_freq):
37+
# GH#22397
38+
ser = Series(range(5), index=date_range("2017", periods=5))
39+
assert ser.shift(freq=move_by_freq) is not ser
40+
2241
def test_shift(self, datetime_series):
2342
shifted = datetime_series.shift(1)
2443
unshifted = shifted.shift(-1)

0 commit comments

Comments
 (0)