Skip to content

TST/REF: collect tests by method #37430

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 3 commits into from
Oct 27, 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
26 changes: 26 additions & 0 deletions pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,32 @@ def test_reset_index_with_drop(
assert len(deleveled.columns) == len(ymd.columns)
assert deleveled.index.name == ymd.index.name

@pytest.mark.parametrize(
"ix_data, exp_data",
[
(
[(pd.NaT, 1), (pd.NaT, 2)],
{"a": [pd.NaT, pd.NaT], "b": [1, 2], "x": [11, 12]},
),
(
[(pd.NaT, 1), (pd.Timestamp("2020-01-01"), 2)],
{"a": [pd.NaT, pd.Timestamp("2020-01-01")], "b": [1, 2], "x": [11, 12]},
),
(
[(pd.NaT, 1), (pd.Timedelta(123, "d"), 2)],
{"a": [pd.NaT, pd.Timedelta(123, "d")], "b": [1, 2], "x": [11, 12]},
),
],
)
def test_reset_index_nat_multiindex(self, ix_data, exp_data):
# GH#36541: that reset_index() does not raise ValueError
ix = MultiIndex.from_tuples(ix_data, names=["a", "b"])
result = DataFrame({"x": [11, 12]}, index=ix)
result = result.reset_index()

expected = DataFrame(exp_data)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"array, dtype",
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,36 @@ def test_loc_preserve_names(self, multiindex_year_month_day_dataframe_random_dat
assert result.index.name == ymd.index.names[2]
assert result2.index.name == ymd.index.names[2]

def test_loc_getitem_multiindex_nonunique_len_zero(self):
# GH#13691
mi = MultiIndex.from_product([[0], [1, 1]])
ser = Series(0, index=mi)

res = ser.loc[[]]

expected = ser[:0]
tm.assert_series_equal(res, expected)

res2 = ser.loc[ser.iloc[0:0]]
tm.assert_series_equal(res2, expected)

def test_loc_getitem_access_none_value_in_multiindex(self):
# GH#34318: test that you can access a None value using .loc
# through a Multiindex

ser = Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]]))
result = ser.loc[("Level1", "Level2")]
assert result is None

midx = MultiIndex.from_product([["Level1"], ["Level2_a", "Level2_b"]])
ser = Series([None] * len(midx), dtype=object, index=midx)
result = ser.loc[("Level1", "Level2_a")]
assert result is None

ser = Series([1] * len(midx), dtype=object, index=midx)
result = ser.loc[("Level1", "Level2_a")]
assert result == 1


def test_series_loc_getitem_label_list_missing_values():
# gh-11428
Expand Down
33 changes: 0 additions & 33 deletions pandas/tests/series/indexing/test_callable.py

This file was deleted.

37 changes: 37 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@


class TestSeriesGetitemScalars:
def test_getitem_keyerror_with_int64index(self):
ser = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])

with pytest.raises(KeyError, match=r"^5$"):
ser[5]

with pytest.raises(KeyError, match=r"^'c'$"):
ser["c"]

# not monotonic
ser = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])

with pytest.raises(KeyError, match=r"^5$"):
ser[5]

with pytest.raises(KeyError, match=r"^'c'$"):
ser["c"]

def test_getitem_int64(self, datetime_series):
idx = np.int64(5)
assert datetime_series[idx] == datetime_series[5]

# TODO: better name/GH ref?
def test_getitem_regression(self):
Expand Down Expand Up @@ -228,6 +249,22 @@ def test_getitem_boolean_different_order(self, string_series):
tm.assert_series_equal(sel, exp)


class TestGetitemCallable:
def test_getitem_callable(self):
# GH#12533
ser = Series(4, index=list("ABCD"))
result = ser[lambda x: "A"]
assert result == ser.loc["A"]

result = ser[lambda x: ["A", "B"]]
expected = ser.loc[["A", "B"]]
tm.assert_series_equal(result, expected)

result = ser[lambda x: [True, False, True, True]]
expected = ser.iloc[[0, 2, 3]]
tm.assert_series_equal(result, expected)


def test_getitem_generator(string_series):
gen = (x > 0 for x in string_series)
result = string_series[gen]
Expand Down
65 changes: 0 additions & 65 deletions pandas/tests/series/indexing/test_multiindex.py

This file was deleted.

24 changes: 0 additions & 24 deletions pandas/tests/series/indexing/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,3 @@ def test_slice_floats2():
s.index = i
assert len(s.loc[12.0:]) == 8
assert len(s.loc[12.5:]) == 7


def test_int_indexing():
s = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])

with pytest.raises(KeyError, match=r"^5$"):
s[5]

with pytest.raises(KeyError, match=r"^'c'$"):
s["c"]

# not monotonic
s = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])

with pytest.raises(KeyError, match=r"^5$"):
s[5]

with pytest.raises(KeyError, match=r"^'c'$"):
s["c"]


def test_getitem_int64(datetime_series):
idx = np.int64(5)
assert datetime_series[idx] == datetime_series[5]
20 changes: 20 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,23 @@ def test_dt64tz_setitem_does_not_mutate_dti(self):
ser[::3] = NaT
assert ser[0] is NaT
assert dti[0] == ts


class TestSetitemCallable:
def test_setitem_callable_key(self):
# GH#12533
ser = Series([1, 2, 3, 4], index=list("ABCD"))
ser[lambda x: "A"] = -1

expected = Series([-1, 2, 3, 4], index=list("ABCD"))
tm.assert_series_equal(ser, expected)

def test_setitem_callable_other(self):
# GH#13299
inc = lambda x: x + 1

ser = Series([1, 2, -1, 4])
ser[ser < 0] = inc

expected = Series([1, 2, inc, 4])
tm.assert_series_equal(ser, expected)