diff --git a/pandas/tests/frame/methods/test_copy.py b/pandas/tests/frame/methods/test_copy.py new file mode 100644 index 0000000000000..be52cf55fccb2 --- /dev/null +++ b/pandas/tests/frame/methods/test_copy.py @@ -0,0 +1,43 @@ +import pytest + +from pandas import DataFrame +import pandas._testing as tm + + +class TestCopy: + @pytest.mark.parametrize("attr", ["index", "columns"]) + def test_copy_index_name_checking(self, float_frame, attr): + # don't want to be able to modify the index stored elsewhere after + # making a copy + ind = getattr(float_frame, attr) + ind.name = None + cp = float_frame.copy() + getattr(cp, attr).name = "foo" + assert getattr(float_frame, attr).name is None + + def test_copy_cache(self): + # GH#31784 _item_cache not cleared on copy causes incorrect reads after updates + df = DataFrame({"a": [1]}) + + df["x"] = [0] + df["a"] + + df.copy() + + df["a"].values[0] = -1 + + tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]})) + + df["y"] = [0] + + assert df["a"].values[0] == -1 + tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]})) + + def test_copy(self, float_frame, float_string_frame): + cop = float_frame.copy() + cop["E"] = cop["A"] + assert "E" not in float_frame + + # copy objects + copy = float_string_frame.copy() + assert copy._mgr is not float_string_frame._mgr diff --git a/pandas/tests/frame/methods/test_to_numpy.py b/pandas/tests/frame/methods/test_to_numpy.py new file mode 100644 index 0000000000000..3d69c004db6bb --- /dev/null +++ b/pandas/tests/frame/methods/test_to_numpy.py @@ -0,0 +1,32 @@ +import numpy as np + +from pandas import DataFrame, Timestamp +import pandas._testing as tm + + +class TestToNumpy: + def test_to_numpy(self): + df = DataFrame({"A": [1, 2], "B": [3, 4.5]}) + expected = np.array([[1, 3], [2, 4.5]]) + result = df.to_numpy() + tm.assert_numpy_array_equal(result, expected) + + def test_to_numpy_dtype(self): + df = DataFrame({"A": [1, 2], "B": [3, 4.5]}) + expected = np.array([[1, 3], [2, 4]], dtype="int64") + result = df.to_numpy(dtype="int64") + tm.assert_numpy_array_equal(result, expected) + + def test_to_numpy_copy(self): + arr = np.random.randn(4, 3) + df = DataFrame(arr) + assert df.values.base is arr + assert df.to_numpy(copy=False).base is arr + assert df.to_numpy(copy=True).base is not arr + + def test_to_numpy_mixed_dtype_to_str(self): + # https://github.com/pandas-dev/pandas/issues/35455 + df = DataFrame([[Timestamp("2020-01-01 00:00:00"), 100.0]]) + result = df.to_numpy(dtype=str) + expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str) + tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 34d56672e5536..a8bc6132159bd 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -16,16 +16,6 @@ class TestDataFrameMisc: - @pytest.mark.parametrize("attr", ["index", "columns"]) - def test_copy_index_name_checking(self, float_frame, attr): - # don't want to be able to modify the index stored elsewhere after - # making a copy - ind = getattr(float_frame, attr) - ind.name = None - cp = float_frame.copy() - getattr(cp, attr).name = "foo" - assert getattr(float_frame, attr).name is None - def test_getitem_pop_assign_name(self, float_frame): s = float_frame["A"] assert s.name == "A" @@ -86,8 +76,7 @@ def test_get_axis(self, float_frame): f._get_axis_number(None) def test_keys(self, float_frame): - getkeys = float_frame.keys - assert getkeys() is float_frame.columns + assert float_frame.keys() is float_frame.columns def test_column_contains_raises(self, float_frame): with pytest.raises(TypeError, match="unhashable type: 'Index'"): @@ -136,15 +125,6 @@ def test_new_empty_index(self): df1.index.name = "foo" assert df2.index.name is None - def test_array_interface(self, float_frame): - with np.errstate(all="ignore"): - result = np.sqrt(float_frame) - assert isinstance(result, type(float_frame)) - assert result.index is float_frame.index - assert result.columns is float_frame.columns - - tm.assert_frame_equal(result, float_frame.apply(np.sqrt)) - def test_get_agg_axis(self, float_frame): cols = float_frame._get_agg_axis(0) assert cols is float_frame.columns @@ -156,7 +136,7 @@ def test_get_agg_axis(self, float_frame): with pytest.raises(ValueError, match=msg): float_frame._get_agg_axis(2) - def test_nonzero(self, float_frame, float_string_frame): + def test_empty(self, float_frame, float_string_frame): empty_frame = DataFrame() assert empty_frame.empty @@ -313,32 +293,6 @@ def test_len(self, float_frame): expected = float_frame.reindex(columns=["A", "B"]).values tm.assert_almost_equal(arr, expected) - def test_to_numpy(self): - df = DataFrame({"A": [1, 2], "B": [3, 4.5]}) - expected = np.array([[1, 3], [2, 4.5]]) - result = df.to_numpy() - tm.assert_numpy_array_equal(result, expected) - - def test_to_numpy_dtype(self): - df = DataFrame({"A": [1, 2], "B": [3, 4.5]}) - expected = np.array([[1, 3], [2, 4]], dtype="int64") - result = df.to_numpy(dtype="int64") - tm.assert_numpy_array_equal(result, expected) - - def test_to_numpy_copy(self): - arr = np.random.randn(4, 3) - df = DataFrame(arr) - assert df.values.base is arr - assert df.to_numpy(copy=False).base is arr - assert df.to_numpy(copy=True).base is not arr - - def test_to_numpy_mixed_dtype_to_str(self): - # https://github.com/pandas-dev/pandas/issues/35455 - df = DataFrame([[pd.Timestamp("2020-01-01 00:00:00"), 100.0]]) - result = df.to_numpy(dtype=str) - expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str) - tm.assert_numpy_array_equal(result, expected) - def test_swapaxes(self): df = DataFrame(np.random.randn(10, 5)) tm.assert_frame_equal(df.T, df.swapaxes(0, 1)) @@ -537,24 +491,6 @@ def test_set_flags(self, allows_duplicate_labels): result.iloc[0, 0] = 10 assert df.iloc[0, 0] == 0 - def test_cache_on_copy(self): - # GH 31784 _item_cache not cleared on copy causes incorrect reads after updates - df = DataFrame({"a": [1]}) - - df["x"] = [0] - df["a"] - - df.copy() - - df["a"].values[0] = -1 - - tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]})) - - df["y"] = [0] - - assert df["a"].values[0] == -1 - tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]})) - @skip_if_no("jinja2") def test_constructor_expanddim_lookup(self): # GH#33628 accessing _constructor_expanddim should not diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index e3aff0df3bab3..34aa11eb76306 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -254,15 +254,6 @@ def f(dtype): if not compat.is_platform_windows(): f("M8[ns]") - def test_copy(self, float_frame, float_string_frame): - cop = float_frame.copy() - cop["E"] = cop["A"] - assert "E" not in float_frame - - # copy objects - copy = float_string_frame.copy() - assert copy._mgr is not float_string_frame._mgr - def test_pickle(self, float_string_frame, timezone_frame): empty_frame = DataFrame() diff --git a/pandas/tests/frame/test_npfuncs.py b/pandas/tests/frame/test_npfuncs.py index a3b4c659a4124..1e37822798244 100644 --- a/pandas/tests/frame/test_npfuncs.py +++ b/pandas/tests/frame/test_npfuncs.py @@ -14,3 +14,12 @@ def test_asarray_homogenous(self): # may change from object in the future expected = np.array([[1, 1], [2, 2]], dtype="object") tm.assert_numpy_array_equal(result, expected) + + def test_np_sqrt(self, float_frame): + with np.errstate(all="ignore"): + result = np.sqrt(float_frame) + assert isinstance(result, type(float_frame)) + assert result.index is float_frame.index + assert result.columns is float_frame.columns + + tm.assert_frame_equal(result, float_frame.apply(np.sqrt)) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_reductions.py similarity index 100% rename from pandas/tests/frame/test_analytics.py rename to pandas/tests/frame/test_reductions.py diff --git a/pandas/tests/frame/test_timezones.py b/pandas/tests/frame/test_timezones.py index 3d814f22ce262..1271a490d6b70 100644 --- a/pandas/tests/frame/test_timezones.py +++ b/pandas/tests/frame/test_timezones.py @@ -58,18 +58,3 @@ def test_boolean_compare_transpose_tzindex_with_dst(self, tz): result = df.T == df.T expected = DataFrame(True, index=list("ab"), columns=idx) tm.assert_frame_equal(result, expected) - - @pytest.mark.parametrize("copy", [True, False]) - @pytest.mark.parametrize( - "method, tz", [["tz_localize", None], ["tz_convert", "Europe/Berlin"]] - ) - def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz): - # GH 6326 - result = DataFrame( - np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz) - ) - getattr(result, method)("UTC", copy=copy) - expected = DataFrame( - np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz) - ) - tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/methods/test_append.py b/pandas/tests/series/methods/test_append.py index 4c2bf4683d17d..069557cc65455 100644 --- a/pandas/tests/series/methods/test_append.py +++ b/pandas/tests/series/methods/test_append.py @@ -7,6 +7,10 @@ class TestSeriesAppend: + def test_append_preserve_name(self, datetime_series): + result = datetime_series[:5].append(datetime_series[5:]) + assert result.name == datetime_series.name + def test_append(self, datetime_series, string_series, object_series): appended_series = string_series.append(object_series) for idx, value in appended_series.items(): diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index c948af41c5e8f..ac8a7b05fd30a 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -26,10 +26,6 @@ class TestSeriesMisc: - def test_append_preserve_name(self, datetime_series): - result = datetime_series[:5].append(datetime_series[5:]) - assert result.name == datetime_series.name - def test_getitem_preserve_name(self, datetime_series): result = datetime_series[datetime_series > 0] assert result.name == datetime_series.name @@ -163,10 +159,7 @@ def test_iter_strings(self, string_series): assert val == string_series[i] def test_keys(self, datetime_series): - # HACK: By doing this in two stages, we avoid 2to3 wrapping the call - # to .keys() in a list() - getkeys = datetime_series.keys - assert getkeys() is datetime_series.index + assert datetime_series.keys() is datetime_series.index def test_values(self, datetime_series): tm.assert_almost_equal( @@ -213,10 +206,6 @@ def test_class_axis(self): # no exception and no empty docstring assert pydoc.getdoc(Series.index) - def test_numpy_unique(self, datetime_series): - # it works! - np.unique(datetime_series) - def test_item(self): s = Series([1]) result = s.item() diff --git a/pandas/tests/series/test_duplicates.py b/pandas/tests/series/test_duplicates.py index 89181a08819b1..672be981fd7d3 100644 --- a/pandas/tests/series/test_duplicates.py +++ b/pandas/tests/series/test_duplicates.py @@ -21,6 +21,11 @@ def test_nunique(): assert s.nunique() == 0 +def test_numpy_unique(datetime_series): + # it works! + np.unique(datetime_series) + + def test_unique(): # GH714 also, dtype=float s = Series([1.2345] * 100)