diff --git a/pandas/tests/frame/methods/test_append.py b/pandas/tests/frame/methods/test_append.py index 133e8c03fab3d..c08a77d00a96d 100644 --- a/pandas/tests/frame/methods/test_append.py +++ b/pandas/tests/frame/methods/test_append.py @@ -7,6 +7,18 @@ class TestDataFrameAppend: + @pytest.mark.parametrize("klass", [Series, DataFrame]) + def test_append_multiindex(self, multiindex_dataframe_random_data, klass): + obj = multiindex_dataframe_random_data + if klass is Series: + obj = obj["A"] + + a = obj[:5] + b = obj[5:] + + result = a.append(b) + tm.assert_equal(result, obj) + def test_append_empty_list(self): # GH 28769 df = DataFrame() diff --git a/pandas/tests/frame/test_convert.py b/pandas/tests/frame/methods/test_convert.py similarity index 100% rename from pandas/tests/frame/test_convert.py rename to pandas/tests/frame/methods/test_convert.py diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index bbcc286d89986..479d7902aa111 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2697,6 +2697,19 @@ def test_frame_ctor_datetime64_column(self): df = DataFrame({"A": np.random.randn(len(rng)), "B": dates}) assert np.issubdtype(df["B"].dtype, np.dtype("M8[ns]")) + def test_dataframe_constructor_infer_multiindex(self): + index_lists = [["a", "a", "b", "b"], ["x", "y", "x", "y"]] + + multi = DataFrame( + np.random.randn(4, 4), + index=[np.array(x) for x in index_lists], + ) + assert isinstance(multi.index, MultiIndex) + assert not isinstance(multi.columns, MultiIndex) + + multi = DataFrame(np.random.randn(4, 4), columns=index_lists) + assert isinstance(multi.columns, MultiIndex) + @pytest.mark.parametrize( "input_vals", [ diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index ec8a690e6e7f9..adbdf1077113e 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1536,6 +1536,18 @@ def test_series_constructor_datetimelike_index_coercion(self): assert ser.index.is_all_dates assert isinstance(ser.index, DatetimeIndex) + def test_series_constructor_infer_multiindex(self): + index_lists = [["a", "a", "b", "b"], ["x", "y", "x", "y"]] + + multi = Series(1.0, index=[np.array(x) for x in index_lists]) + assert isinstance(multi.index, MultiIndex) + + multi = Series(1.0, index=index_lists) + assert isinstance(multi.index, MultiIndex) + + multi = Series(range(4), index=index_lists) + assert isinstance(multi.index, MultiIndex) + class TestSeriesConstructorInternals: def test_constructor_no_pandas_array(self): diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 901049209bf64..6f5345f802a7c 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -21,42 +21,6 @@ class TestMultiLevel: - def test_append(self, multiindex_dataframe_random_data): - frame = multiindex_dataframe_random_data - - a, b = frame[:5], frame[5:] - - result = a.append(b) - tm.assert_frame_equal(result, frame) - - result = a["A"].append(b["A"]) - tm.assert_series_equal(result, frame["A"]) - - def test_dataframe_constructor_infer_multiindex(self): - multi = DataFrame( - np.random.randn(4, 4), - index=[np.array(["a", "a", "b", "b"]), np.array(["x", "y", "x", "y"])], - ) - assert isinstance(multi.index, MultiIndex) - assert not isinstance(multi.columns, MultiIndex) - - multi = DataFrame( - np.random.randn(4, 4), columns=[["a", "a", "b", "b"], ["x", "y", "x", "y"]] - ) - assert isinstance(multi.columns, MultiIndex) - - def test_series_constructor_infer_multiindex(self): - multi = Series( - 1.0, index=[np.array(["a", "a", "b", "b"]), np.array(["x", "y", "x", "y"])] - ) - assert isinstance(multi.index, MultiIndex) - - multi = Series(1.0, index=[["a", "a", "b", "b"], ["x", "y", "x", "y"]]) - assert isinstance(multi.index, MultiIndex) - - multi = Series(range(4), index=[["a", "a", "b", "b"], ["x", "y", "x", "y"]]) - assert isinstance(multi.index, MultiIndex) - def test_reindex_level(self, multiindex_year_month_day_dataframe_random_data): # axis=0 ymd = multiindex_year_month_day_dataframe_random_data @@ -278,18 +242,17 @@ def test_std_var_pass_ddof(self): expected = df.groupby(level=0).agg(alt) tm.assert_frame_equal(result, expected) - def test_frame_series_agg_multiple_levels( - self, multiindex_year_month_day_dataframe_random_data + @pytest.mark.parametrize("klass", [Series, DataFrame]) + def test_agg_multiple_levels( + self, multiindex_year_month_day_dataframe_random_data, klass ): ymd = multiindex_year_month_day_dataframe_random_data + if klass is Series: + ymd = ymd["A"] result = ymd.sum(level=["year", "month"]) expected = ymd.groupby(level=["year", "month"]).sum() - tm.assert_frame_equal(result, expected) - - result = ymd["A"].sum(level=["year", "month"]) - expected = ymd["A"].groupby(level=["year", "month"]).sum() - tm.assert_series_equal(result, expected) + tm.assert_equal(result, expected) def test_groupby_multilevel(self, multiindex_year_month_day_dataframe_random_data): ymd = multiindex_year_month_day_dataframe_random_data