diff --git a/pandas/tests/frame/indexing/test_at.py b/pandas/tests/indexing/test_at.py similarity index 100% rename from pandas/tests/frame/indexing/test_at.py rename to pandas/tests/indexing/test_at.py diff --git a/pandas/tests/frame/indexing/test_iat.py b/pandas/tests/indexing/test_iat.py similarity index 100% rename from pandas/tests/frame/indexing/test_iat.py rename to pandas/tests/indexing/test_iat.py diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 4ef6463fd9e31..867941a97b598 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -776,3 +776,31 @@ def test_iloc_setitem_series_duplicate_columns(self): ) df.iloc[:, 0] = df.iloc[:, 0].astype(np.float64) assert df.dtypes.iloc[2] == np.int64 + + +class TestILocSeries: + def test_iloc(self): + ser = Series(np.random.randn(10), index=list(range(0, 20, 2))) + + for i in range(len(ser)): + result = ser.iloc[i] + exp = ser[ser.index[i]] + tm.assert_almost_equal(result, exp) + + # pass a slice + result = ser.iloc[slice(1, 3)] + expected = ser.loc[2:4] + tm.assert_series_equal(result, expected) + + # test slice is a view + result[:] = 0 + assert (ser[1:3] == 0).all() + + # list of integers + result = ser.iloc[[0, 2, 3, 4, 5]] + expected = ser.reindex(ser.index[[0, 2, 3, 4, 5]]) + tm.assert_series_equal(result, expected) + + def test_iloc_getitem_nonunique(self): + ser = Series([0, 1, 2], index=[0, 1, 0]) + assert ser.iloc[2] == 2 diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 8fb418ab78307..7748392a893eb 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1212,3 +1212,153 @@ def test_loc_with_period_index_indexer(): tm.assert_frame_equal(df, df.loc[list(idx)]) tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) tm.assert_frame_equal(df, df.loc[list(idx)]) + + +class TestLocSeries: + @pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)]) + def test_loc_uint64(self, val, expected): + # see GH#19399 + ser = Series({2 ** 63 - 1: 3, 2 ** 63: 4}) + assert ser.loc[val] == expected + + def test_loc_getitem(self, string_series, datetime_series): + inds = string_series.index[[3, 4, 7]] + tm.assert_series_equal(string_series.loc[inds], string_series.reindex(inds)) + tm.assert_series_equal(string_series.iloc[5::2], string_series[5::2]) + + # slice with indices + d1, d2 = datetime_series.index[[5, 15]] + result = datetime_series.loc[d1:d2] + expected = datetime_series.truncate(d1, d2) + tm.assert_series_equal(result, expected) + + # boolean + mask = string_series > string_series.median() + tm.assert_series_equal(string_series.loc[mask], string_series[mask]) + + # ask for index value + assert datetime_series.loc[d1] == datetime_series[d1] + assert datetime_series.loc[d2] == datetime_series[d2] + + def test_loc_getitem_not_monotonic(self, datetime_series): + d1, d2 = datetime_series.index[[5, 15]] + + ts2 = datetime_series[::2][[1, 2, 0]] + + msg = r"Timestamp\('2000-01-10 00:00:00'\)" + with pytest.raises(KeyError, match=msg): + ts2.loc[d1:d2] + with pytest.raises(KeyError, match=msg): + ts2.loc[d1:d2] = 0 + + def test_loc_getitem_setitem_integer_slice_keyerrors(self): + ser = Series(np.random.randn(10), index=list(range(0, 20, 2))) + + # this is OK + cp = ser.copy() + cp.iloc[4:10] = 0 + assert (cp.iloc[4:10] == 0).all() + + # so is this + cp = ser.copy() + cp.iloc[3:11] = 0 + assert (cp.iloc[3:11] == 0).values.all() + + result = ser.iloc[2:6] + result2 = ser.loc[3:11] + expected = ser.reindex([4, 6, 8, 10]) + + tm.assert_series_equal(result, expected) + tm.assert_series_equal(result2, expected) + + # non-monotonic, raise KeyError + s2 = ser.iloc[list(range(5)) + list(range(9, 4, -1))] + with pytest.raises(KeyError, match=r"^3$"): + s2.loc[3:11] + with pytest.raises(KeyError, match=r"^3$"): + s2.loc[3:11] = 0 + + def test_loc_getitem_iterator(self, string_series): + idx = iter(string_series.index[:10]) + result = string_series.loc[idx] + tm.assert_series_equal(result, string_series[:10]) + + def test_loc_setitem_boolean(self, string_series): + mask = string_series > string_series.median() + + result = string_series.copy() + result.loc[mask] = 0 + expected = string_series + expected[mask] = 0 + tm.assert_series_equal(result, expected) + + def test_loc_setitem_corner(self, string_series): + inds = list(string_series.index[[5, 8, 12]]) + string_series.loc[inds] = 5 + msg = r"\['foo'\] not in index" + with pytest.raises(KeyError, match=msg): + string_series.loc[inds + ["foo"]] = 5 + + def test_basic_setitem_with_labels(self, datetime_series): + indices = datetime_series.index[[5, 10, 15]] + + cp = datetime_series.copy() + exp = datetime_series.copy() + cp[indices] = 0 + exp.loc[indices] = 0 + tm.assert_series_equal(cp, exp) + + cp = datetime_series.copy() + exp = datetime_series.copy() + cp[indices[0] : indices[2]] = 0 + exp.loc[indices[0] : indices[2]] = 0 + tm.assert_series_equal(cp, exp) + + def test_loc_setitem_listlike_of_ints(self): + + # integer indexes, be careful + ser = Series(np.random.randn(10), index=list(range(0, 20, 2))) + inds = [0, 4, 6] + arr_inds = np.array([0, 4, 6]) + + cp = ser.copy() + exp = ser.copy() + ser[inds] = 0 + ser.loc[inds] = 0 + tm.assert_series_equal(cp, exp) + + cp = ser.copy() + exp = ser.copy() + ser[arr_inds] = 0 + ser.loc[arr_inds] = 0 + tm.assert_series_equal(cp, exp) + + inds_notfound = [0, 4, 5, 6] + arr_inds_notfound = np.array([0, 4, 5, 6]) + msg = r"\[5\] not in index" + with pytest.raises(KeyError, match=msg): + ser[inds_notfound] = 0 + with pytest.raises(Exception, match=msg): + ser[arr_inds_notfound] = 0 + + def test_loc_setitem_dt64tz_values(self): + # GH#12089 + ser = Series( + date_range("2011-01-01", periods=3, tz="US/Eastern"), + index=["a", "b", "c"], + ) + s2 = ser.copy() + expected = Timestamp("2011-01-03", tz="US/Eastern") + s2.loc["a"] = expected + result = s2.loc["a"] + assert result == expected + + s2 = ser.copy() + s2.iloc[0] = expected + result = s2.iloc[0] + assert result == expected + + s2 = ser.copy() + s2["a"] = expected + result = s2["a"] + assert result == expected diff --git a/pandas/tests/series/indexing/test_iloc.py b/pandas/tests/series/indexing/test_iloc.py deleted file mode 100644 index f276eb5b0b23d..0000000000000 --- a/pandas/tests/series/indexing/test_iloc.py +++ /dev/null @@ -1,32 +0,0 @@ -import numpy as np - -from pandas import Series -import pandas._testing as tm - - -def test_iloc(): - s = Series(np.random.randn(10), index=list(range(0, 20, 2))) - - for i in range(len(s)): - result = s.iloc[i] - exp = s[s.index[i]] - tm.assert_almost_equal(result, exp) - - # pass a slice - result = s.iloc[slice(1, 3)] - expected = s.loc[2:4] - tm.assert_series_equal(result, expected) - - # test slice is a view - result[:] = 0 - assert (s[1:3] == 0).all() - - # list of integers - result = s.iloc[[0, 2, 3, 4, 5]] - expected = s.reindex(s.index[[0, 2, 3, 4, 5]]) - tm.assert_series_equal(result, expected) - - -def test_iloc_nonunique(): - s = Series([0, 1, 2], index=[0, 1, 0]) - assert s.iloc[2] == 2 diff --git a/pandas/tests/series/indexing/test_loc.py b/pandas/tests/series/indexing/test_loc.py deleted file mode 100644 index 368adcfb32215..0000000000000 --- a/pandas/tests/series/indexing/test_loc.py +++ /dev/null @@ -1,159 +0,0 @@ -import numpy as np -import pytest - -import pandas as pd -from pandas import Series, Timestamp -import pandas._testing as tm - - -@pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)]) -def test_loc_uint64(val, expected): - # see gh-19399 - s = Series({2 ** 63 - 1: 3, 2 ** 63: 4}) - assert s.loc[val] == expected - - -def test_loc_getitem(string_series, datetime_series): - inds = string_series.index[[3, 4, 7]] - tm.assert_series_equal(string_series.loc[inds], string_series.reindex(inds)) - tm.assert_series_equal(string_series.iloc[5::2], string_series[5::2]) - - # slice with indices - d1, d2 = datetime_series.index[[5, 15]] - result = datetime_series.loc[d1:d2] - expected = datetime_series.truncate(d1, d2) - tm.assert_series_equal(result, expected) - - # boolean - mask = string_series > string_series.median() - tm.assert_series_equal(string_series.loc[mask], string_series[mask]) - - # ask for index value - assert datetime_series.loc[d1] == datetime_series[d1] - assert datetime_series.loc[d2] == datetime_series[d2] - - -def test_loc_getitem_not_monotonic(datetime_series): - d1, d2 = datetime_series.index[[5, 15]] - - ts2 = datetime_series[::2][[1, 2, 0]] - - msg = r"Timestamp\('2000-01-10 00:00:00'\)" - with pytest.raises(KeyError, match=msg): - ts2.loc[d1:d2] - with pytest.raises(KeyError, match=msg): - ts2.loc[d1:d2] = 0 - - -def test_loc_getitem_setitem_integer_slice_keyerrors(): - s = Series(np.random.randn(10), index=list(range(0, 20, 2))) - - # this is OK - cp = s.copy() - cp.iloc[4:10] = 0 - assert (cp.iloc[4:10] == 0).all() - - # so is this - cp = s.copy() - cp.iloc[3:11] = 0 - assert (cp.iloc[3:11] == 0).values.all() - - result = s.iloc[2:6] - result2 = s.loc[3:11] - expected = s.reindex([4, 6, 8, 10]) - - tm.assert_series_equal(result, expected) - tm.assert_series_equal(result2, expected) - - # non-monotonic, raise KeyError - s2 = s.iloc[list(range(5)) + list(range(9, 4, -1))] - with pytest.raises(KeyError, match=r"^3$"): - s2.loc[3:11] - with pytest.raises(KeyError, match=r"^3$"): - s2.loc[3:11] = 0 - - -def test_loc_getitem_iterator(string_series): - idx = iter(string_series.index[:10]) - result = string_series.loc[idx] - tm.assert_series_equal(result, string_series[:10]) - - -def test_loc_setitem_boolean(string_series): - mask = string_series > string_series.median() - - result = string_series.copy() - result.loc[mask] = 0 - expected = string_series - expected[mask] = 0 - tm.assert_series_equal(result, expected) - - -def test_loc_setitem_corner(string_series): - inds = list(string_series.index[[5, 8, 12]]) - string_series.loc[inds] = 5 - msg = r"\['foo'\] not in index" - with pytest.raises(KeyError, match=msg): - string_series.loc[inds + ["foo"]] = 5 - - -def test_basic_setitem_with_labels(datetime_series): - indices = datetime_series.index[[5, 10, 15]] - - cp = datetime_series.copy() - exp = datetime_series.copy() - cp[indices] = 0 - exp.loc[indices] = 0 - tm.assert_series_equal(cp, exp) - - cp = datetime_series.copy() - exp = datetime_series.copy() - cp[indices[0] : indices[2]] = 0 - exp.loc[indices[0] : indices[2]] = 0 - tm.assert_series_equal(cp, exp) - - # integer indexes, be careful - s = Series(np.random.randn(10), index=list(range(0, 20, 2))) - inds = [0, 4, 6] - arr_inds = np.array([0, 4, 6]) - - cp = s.copy() - exp = s.copy() - s[inds] = 0 - s.loc[inds] = 0 - tm.assert_series_equal(cp, exp) - - cp = s.copy() - exp = s.copy() - s[arr_inds] = 0 - s.loc[arr_inds] = 0 - tm.assert_series_equal(cp, exp) - - inds_notfound = [0, 4, 5, 6] - arr_inds_notfound = np.array([0, 4, 5, 6]) - msg = r"\[5\] not in index" - with pytest.raises(KeyError, match=msg): - s[inds_notfound] = 0 - with pytest.raises(Exception, match=msg): - s[arr_inds_notfound] = 0 - - # GH12089 - # with tz for values - s = Series( - pd.date_range("2011-01-01", periods=3, tz="US/Eastern"), index=["a", "b", "c"] - ) - s2 = s.copy() - expected = Timestamp("2011-01-03", tz="US/Eastern") - s2.loc["a"] = expected - result = s2.loc["a"] - assert result == expected - - s2 = s.copy() - s2.iloc[0] = expected - result = s2.iloc[0] - assert result == expected - - s2 = s.copy() - s2["a"] = expected - result = s2["a"] - assert result == expected