diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index d301ed969789e..a5f5e6f36cd58 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -364,14 +364,14 @@ def test_pickle(self, float_string_frame, timezone_frame): def test_consolidate_datetime64(self): # numpy vstack bug - data = """\ -starting,ending,measure -2012-06-21 00:00,2012-06-23 07:00,77 -2012-06-23 07:00,2012-06-23 16:30,65 -2012-06-23 16:30,2012-06-25 08:00,77 -2012-06-25 08:00,2012-06-26 12:00,0 -2012-06-26 12:00,2012-06-27 08:00,77 -""" + data = ( + "starting,ending,measure\n" + "2012-06-21 00:00,2012-06-23 07:00,77\n" + "2012-06-23 07:00,2012-06-23 16:30,65\n" + "2012-06-23 16:30,2012-06-25 08:00,77\n" + "2012-06-25 08:00,2012-06-26 12:00,0\n" + "2012-06-26 12:00,2012-06-27 08:00,77\n" + ) df = pd.read_csv(StringIO(data), parse_dates=[0, 1]) ser_starting = df.starting @@ -397,9 +397,6 @@ def test_is_mixed_type(self, float_frame, float_string_frame): assert float_string_frame._is_mixed_type def test_get_numeric_data(self): - # TODO(wesm): unused? - intname = np.dtype(np.int_).name # noqa - floatname = np.dtype(np.float_).name # noqa datetime64name = np.dtype("M8[ns]").name objectname = np.dtype(np.object_).name @@ -581,6 +578,7 @@ def test_get_X_columns(self): tm.assert_index_equal(df._get_numeric_data().columns, pd.Index(["a", "b", "e"])) def test_strange_column_corruption_issue(self): + # FIXME: dont leave commented-out # (wesm) Unclear how exactly this is related to internal matters df = DataFrame(index=[0, 1]) df[0] = np.nan diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index a7e01d8f1fd6d..4ac009ef508c4 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -129,9 +129,6 @@ def test_repr_unsortable(self, float_frame): def test_repr_unicode(self): uval = "\u03c3\u03c3\u03c3\u03c3" - # TODO(wesm): is this supposed to be used? - bval = uval.encode("utf-8") # noqa - df = DataFrame({"A": [uval, uval]}) result = repr(df) diff --git a/pandas/tests/generic/test_frame.py b/pandas/tests/generic/test_frame.py index 7fe22e77c5bf3..d8f4257566f84 100644 --- a/pandas/tests/generic/test_frame.py +++ b/pandas/tests/generic/test_frame.py @@ -160,7 +160,7 @@ def finalize(self, other, method=None, **kwargs): # reset DataFrame._metadata = _metadata - DataFrame.__finalize__ = _finalize + DataFrame.__finalize__ = _finalize # FIXME: use monkeypatch def test_set_attribute(self): # Test for consistent setattr behavior when an attribute and a column @@ -174,6 +174,69 @@ def test_set_attribute(self): assert df.y == 5 tm.assert_series_equal(df["y"], Series([2, 4, 6], name="y")) + def test_deepcopy_empty(self): + # This test covers empty frame copying with non-empty column sets + # as reported in issue GH15370 + empty_frame = DataFrame(data=[], index=[], columns=["A"]) + empty_frame_copy = deepcopy(empty_frame) + + self._compare(empty_frame_copy, empty_frame) + + +# formerly in Generic but only test DataFrame +class TestDataFrame2: + def test_validate_bool_args(self): + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) + invalid_values = [1, "True", [1, 2, 3], 5.0] + + for value in invalid_values: + with pytest.raises(ValueError): + super(DataFrame, df).rename_axis( + mapper={"a": "x", "b": "y"}, axis=1, inplace=value + ) + + with pytest.raises(ValueError): + super(DataFrame, df).drop("a", axis=1, inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df)._consolidate(inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df).fillna(value=0, inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df).replace(to_replace=1, value=7, inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df).interpolate(inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df)._where(cond=df.a > 2, inplace=value) + + with pytest.raises(ValueError): + super(DataFrame, df).mask(cond=df.a > 2, inplace=value) + + def test_unexpected_keyword(self): + # GH8597 + df = DataFrame(np.random.randn(5, 2), columns=["jim", "joe"]) + ca = pd.Categorical([0, 0, 2, 2, 3, np.nan]) + ts = df["joe"].copy() + ts[2] = np.nan + + with pytest.raises(TypeError, match="unexpected keyword"): + df.drop("joe", axis=1, in_place=True) + + with pytest.raises(TypeError, match="unexpected keyword"): + df.reindex([1, 0], inplace=True) + + with pytest.raises(TypeError, match="unexpected keyword"): + ca.fillna(0, inplace=True) + + with pytest.raises(TypeError, match="unexpected keyword"): + ts.fillna(0, in_place=True) + + +class TestToXArray: @pytest.mark.skipif( not _XARRAY_INSTALLED or _XARRAY_INSTALLED @@ -272,11 +335,3 @@ def test_to_xarray(self): expected["f"] = expected["f"].astype(object) expected.columns.name = None tm.assert_frame_equal(result, expected, check_index_type=False) - - def test_deepcopy_empty(self): - # This test covers empty frame copying with non-empty column sets - # as reported in issue GH15370 - empty_frame = DataFrame(data=[], index=[], columns=["A"]) - empty_frame_copy = deepcopy(empty_frame) - - self._compare(empty_frame_copy, empty_frame) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 7645c6b4cf709..d574660d21c0d 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -103,23 +103,6 @@ def test_get_numeric_data(self): # _get_numeric_data is includes _get_bool_data, so can't test for # non-inclusion - def test_get_default(self): - - # GH 7725 - d0 = "a", "b", "c", "d" - d1 = np.arange(4, dtype="int64") - others = "e", 10 - - for data, index in ((d0, d1), (d1, d0)): - s = Series(data, index=index) - for i, d in zip(index, data): - assert s.get(i) == d - assert s.get(i, d) == d - assert s.get(i, "z") == d - for other in others: - assert s.get(other, "z") == "z" - assert s.get(other, other) == other - def test_nonzero(self): # GH 4633 @@ -469,24 +452,6 @@ def test_split_compat(self): assert len(np.array_split(o, 5)) == 5 assert len(np.array_split(o, 2)) == 2 - def test_unexpected_keyword(self): # GH8597 - df = DataFrame(np.random.randn(5, 2), columns=["jim", "joe"]) - ca = pd.Categorical([0, 0, 2, 2, 3, np.nan]) - ts = df["joe"].copy() - ts[2] = np.nan - - with pytest.raises(TypeError, match="unexpected keyword"): - df.drop("joe", axis=1, in_place=True) - - with pytest.raises(TypeError, match="unexpected keyword"): - df.reindex([1, 0], inplace=True) - - with pytest.raises(TypeError, match="unexpected keyword"): - ca.fillna(0, inplace=True) - - with pytest.raises(TypeError, match="unexpected keyword"): - ts.fillna(0, in_place=True) - # See gh-12301 def test_stat_unexpected_keyword(self): obj = self._construct(5) @@ -544,37 +509,6 @@ def test_truncate_out_of_bounds(self): self._compare(big.truncate(before=0, after=3e6), big) self._compare(big.truncate(before=-1, after=2e6), big) - def test_validate_bool_args(self): - df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) - invalid_values = [1, "True", [1, 2, 3], 5.0] - - for value in invalid_values: - with pytest.raises(ValueError): - super(DataFrame, df).rename_axis( - mapper={"a": "x", "b": "y"}, axis=1, inplace=value - ) - - with pytest.raises(ValueError): - super(DataFrame, df).drop("a", axis=1, inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df)._consolidate(inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df).fillna(value=0, inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df).replace(to_replace=1, value=7, inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df).interpolate(inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df)._where(cond=df.a > 2, inplace=value) - - with pytest.raises(ValueError): - super(DataFrame, df).mask(cond=df.a > 2, inplace=value) - def test_copy_and_deepcopy(self): # GH 15444 for shape in [0, 1, 2]: diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 8ad8355f2d530..ce0daf8522687 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -181,8 +181,49 @@ def finalize(self, other, method=None, **kwargs): # reset Series._metadata = _metadata - Series.__finalize__ = _finalize + Series.__finalize__ = _finalize # FIXME: use monkeypatch + @pytest.mark.parametrize( + "s", + [ + Series([np.arange(5)]), + pd.date_range("1/1/2011", periods=24, freq="H"), + pd.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 = pd.Series(range(5), index=pd.date_range("2017", periods=5)) + assert s.shift(freq=move_by_freq) is not s + + +class TestSeries2: + # moved from Generic + def test_get_default(self): + + # GH#7725 + d0 = ["a", "b", "c", "d"] + d1 = np.arange(4, dtype="int64") + others = ["e", 10] + + for data, index in ((d0, d1), (d1, d0)): + s = Series(data, index=index) + for i, d in zip(index, data): + assert s.get(i) == d + assert s.get(i, d) == d + assert s.get(i, "z") == d + for other in others: + assert s.get(other, "z") == "z" + assert s.get(other, other) == other + + +class TestToXArray: @pytest.mark.skipif( not _XARRAY_INSTALLED or _XARRAY_INSTALLED @@ -242,22 +283,3 @@ def test_to_xarray(self): tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"]) assert isinstance(result, DataArray) tm.assert_series_equal(result.to_series(), s) - - @pytest.mark.parametrize( - "s", - [ - Series([np.arange(5)]), - pd.date_range("1/1/2011", periods=24, freq="H"), - pd.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 = pd.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/test_compat.py b/pandas/tests/test_compat.py deleted file mode 100644 index 4ff8b0b31e85e..0000000000000 --- a/pandas/tests/test_compat.py +++ /dev/null @@ -1,3 +0,0 @@ -""" -Testing that functions from compat work as expected -"""