diff --git a/pandas/tests/frame/methods/test_rename.py b/pandas/tests/frame/methods/test_rename.py index eb908e9472fe2..ccd365044942a 100644 --- a/pandas/tests/frame/methods/test_rename.py +++ b/pandas/tests/frame/methods/test_rename.py @@ -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"} diff --git a/pandas/tests/frame/methods/test_sample.py b/pandas/tests/frame/methods/test_sample.py new file mode 100644 index 0000000000000..843c44bcf1471 --- /dev/null +++ b/pandas/tests/frame/methods/test_sample.py @@ -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 diff --git a/pandas/tests/generic/methods/test_pipe.py b/pandas/tests/generic/methods/test_pipe.py new file mode 100644 index 0000000000000..59e5edc4b8bb5 --- /dev/null +++ b/pandas/tests/generic/methods/test_pipe.py @@ -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) diff --git a/pandas/tests/generic/test_frame.py b/pandas/tests/generic/test_frame.py index 9ecc0e6194912..da02a82890adc 100644 --- a/pandas/tests/generic/test_frame.py +++ b/pandas/tests/generic/test_frame.py @@ -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]]) @@ -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): # groupby df = DataFrame( { @@ -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), @@ -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 diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index bc666ade9f13d..335f5125faa91 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -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 @@ -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 @@ -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): + def test_sample(self): # Fixes issue: 2419 # additional specific object based tests @@ -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()]: @@ -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) diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 0f8df5bb78304..0a05a42f0fc39 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -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( @@ -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), @@ -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"] @@ -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 diff --git a/pandas/tests/series/methods/test_shift.py b/pandas/tests/series/methods/test_shift.py index 20bb3e008c792..4b23820caeeb4 100644 --- a/pandas/tests/series/methods/test_shift.py +++ b/pandas/tests/series/methods/test_shift.py @@ -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)