Skip to content

Commit

Permalink
TST/REF: collect multilevel tests by method (pandas-dev#37431)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and Kevin D Smith committed Nov 2, 2020
1 parent abaa2eb commit 1c3b1aa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
49 changes: 6 additions & 43 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1c3b1aa

Please sign in to comment.