Skip to content

TST/REF: collect tests from test_multilevel #38006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@


class TestDataFrameReprInfoEtc:
def test_repr_unicode_level_names(self, frame_or_series):
index = MultiIndex.from_tuples([(0, 0), (1, 1)], names=["\u0394", "i1"])

obj = DataFrame(np.random.randn(2, 4), index=index)
if frame_or_series is Series:
obj = obj[0]
repr(obj)

def test_assign_index_sequences(self):
# GH#2200
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,40 @@ def test_loc_getitem_slice_datetime_objs_with_datetimeindex(self):
result = ser.loc[datetime(1900, 1, 1) : datetime(2100, 1, 1)]
tm.assert_series_equal(result, ser)

def test_loc_getitem_sorted_index_level_with_duplicates(self):
# GH#4516 sorting a MultiIndex with duplicates and multiple dtypes
mi = MultiIndex.from_tuples(
[
("foo", "bar"),
("foo", "bar"),
("bah", "bam"),
("bah", "bam"),
("foo", "bar"),
("bah", "bam"),
],
names=["A", "B"],
)
df = DataFrame(
[
[1.0, 1],
[2.0, 2],
[3.0, 3],
[4.0, 4],
[5.0, 5],
[6.0, 6],
],
index=mi,
columns=["C", "D"],
)
df = df.sort_index(level=0)

expected = DataFrame(
[[1.0, 1], [2.0, 2], [5.0, 5]], columns=["C", "D"], index=mi.take([0, 1, 4])
)

result = df.loc[("foo", "bar")]
tm.assert_frame_equal(result, expected)


class TestLocSetitemWithExpansion:
@pytest.mark.slow
Expand Down
39 changes: 5 additions & 34 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def test_groupby_level_no_obs(self):
result = grouped.sum()
assert (result.columns == ["f2", "f3"]).all()

def test_insert_index(self, multiindex_year_month_day_dataframe_random_data):
def test_setitem_with_expansion_multiindex_columns(
self, multiindex_year_month_day_dataframe_random_data
):
ymd = multiindex_year_month_day_dataframe_random_data

df = ymd[:5].T
Expand Down Expand Up @@ -242,12 +244,11 @@ def test_std_var_pass_ddof(self):
expected = df.groupby(level=0).agg(alt)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("klass", [Series, DataFrame])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we ought to make some rules to prohibit this in favor of the fixture (maybe not be perfect but can prob catch most of these)

def test_agg_multiple_levels(
self, multiindex_year_month_day_dataframe_random_data, klass
self, multiindex_year_month_day_dataframe_random_data, frame_or_series
):
ymd = multiindex_year_month_day_dataframe_random_data
if klass is Series:
if frame_or_series is Series:
ymd = ymd["A"]

result = ymd.sum(level=["year", "month"])
Expand Down Expand Up @@ -349,14 +350,6 @@ def test_reindex_level_partial_selection(self, multiindex_dataframe_random_data)
result = frame.T.loc[:, ["foo", "qux"]]
tm.assert_frame_equal(result, expected.T)

def test_unicode_repr_level_names(self):
index = MultiIndex.from_tuples([(0, 0), (1, 1)], names=["\u0394", "i1"])

s = Series(range(2), index=index)
df = DataFrame(np.random.randn(2, 4), index=index)
repr(s)
repr(df)

@pytest.mark.parametrize("d", [4, "d"])
def test_empty_frame_groupby_dtypes_consistency(self, d):
# GH 20888
Expand Down Expand Up @@ -386,28 +379,6 @@ def test_duplicate_groupby_issues(self):
result = s.groupby(s.index).first()
assert len(result) == 3

def test_duplicate_mi(self):
# GH 4516
df = DataFrame(
[
["foo", "bar", 1.0, 1],
["foo", "bar", 2.0, 2],
["bah", "bam", 3.0, 3],
["bah", "bam", 4.0, 4],
["foo", "bar", 5.0, 5],
["bah", "bam", 6.0, 6],
],
columns=list("ABCD"),
)
df = df.set_index(["A", "B"])
df = df.sort_index(level=0)
expected = DataFrame(
[["foo", "bar", 1.0, 1], ["foo", "bar", 2.0, 2], ["foo", "bar", 5.0, 5]],
columns=list("ABCD"),
).set_index(["A", "B"])
result = df.loc[("foo", "bar")]
tm.assert_frame_equal(result, expected)

def test_subsets_multiindex_dtype(self):
# GH 20757
data = [["x", 1]]
Expand Down