Skip to content

Commit a6311fb

Browse files
authored
TST/REF: loc/iloc/at/iat tests go in tests.indexing (#37487)
1 parent 792cc46 commit a6311fb

File tree

6 files changed

+178
-191
lines changed

6 files changed

+178
-191
lines changed

pandas/tests/indexing/test_iloc.py

+28
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,31 @@ def test_iloc_setitem_series_duplicate_columns(self):
776776
)
777777
df.iloc[:, 0] = df.iloc[:, 0].astype(np.float64)
778778
assert df.dtypes.iloc[2] == np.int64
779+
780+
781+
class TestILocSeries:
782+
def test_iloc(self):
783+
ser = Series(np.random.randn(10), index=list(range(0, 20, 2)))
784+
785+
for i in range(len(ser)):
786+
result = ser.iloc[i]
787+
exp = ser[ser.index[i]]
788+
tm.assert_almost_equal(result, exp)
789+
790+
# pass a slice
791+
result = ser.iloc[slice(1, 3)]
792+
expected = ser.loc[2:4]
793+
tm.assert_series_equal(result, expected)
794+
795+
# test slice is a view
796+
result[:] = 0
797+
assert (ser[1:3] == 0).all()
798+
799+
# list of integers
800+
result = ser.iloc[[0, 2, 3, 4, 5]]
801+
expected = ser.reindex(ser.index[[0, 2, 3, 4, 5]])
802+
tm.assert_series_equal(result, expected)
803+
804+
def test_iloc_getitem_nonunique(self):
805+
ser = Series([0, 1, 2], index=[0, 1, 0])
806+
assert ser.iloc[2] == 2

pandas/tests/indexing/test_loc.py

+150
Original file line numberDiff line numberDiff line change
@@ -1213,3 +1213,153 @@ def test_loc_with_period_index_indexer():
12131213
tm.assert_frame_equal(df, df.loc[list(idx)])
12141214
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
12151215
tm.assert_frame_equal(df, df.loc[list(idx)])
1216+
1217+
1218+
class TestLocSeries:
1219+
@pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)])
1220+
def test_loc_uint64(self, val, expected):
1221+
# see GH#19399
1222+
ser = Series({2 ** 63 - 1: 3, 2 ** 63: 4})
1223+
assert ser.loc[val] == expected
1224+
1225+
def test_loc_getitem(self, string_series, datetime_series):
1226+
inds = string_series.index[[3, 4, 7]]
1227+
tm.assert_series_equal(string_series.loc[inds], string_series.reindex(inds))
1228+
tm.assert_series_equal(string_series.iloc[5::2], string_series[5::2])
1229+
1230+
# slice with indices
1231+
d1, d2 = datetime_series.index[[5, 15]]
1232+
result = datetime_series.loc[d1:d2]
1233+
expected = datetime_series.truncate(d1, d2)
1234+
tm.assert_series_equal(result, expected)
1235+
1236+
# boolean
1237+
mask = string_series > string_series.median()
1238+
tm.assert_series_equal(string_series.loc[mask], string_series[mask])
1239+
1240+
# ask for index value
1241+
assert datetime_series.loc[d1] == datetime_series[d1]
1242+
assert datetime_series.loc[d2] == datetime_series[d2]
1243+
1244+
def test_loc_getitem_not_monotonic(self, datetime_series):
1245+
d1, d2 = datetime_series.index[[5, 15]]
1246+
1247+
ts2 = datetime_series[::2][[1, 2, 0]]
1248+
1249+
msg = r"Timestamp\('2000-01-10 00:00:00'\)"
1250+
with pytest.raises(KeyError, match=msg):
1251+
ts2.loc[d1:d2]
1252+
with pytest.raises(KeyError, match=msg):
1253+
ts2.loc[d1:d2] = 0
1254+
1255+
def test_loc_getitem_setitem_integer_slice_keyerrors(self):
1256+
ser = Series(np.random.randn(10), index=list(range(0, 20, 2)))
1257+
1258+
# this is OK
1259+
cp = ser.copy()
1260+
cp.iloc[4:10] = 0
1261+
assert (cp.iloc[4:10] == 0).all()
1262+
1263+
# so is this
1264+
cp = ser.copy()
1265+
cp.iloc[3:11] = 0
1266+
assert (cp.iloc[3:11] == 0).values.all()
1267+
1268+
result = ser.iloc[2:6]
1269+
result2 = ser.loc[3:11]
1270+
expected = ser.reindex([4, 6, 8, 10])
1271+
1272+
tm.assert_series_equal(result, expected)
1273+
tm.assert_series_equal(result2, expected)
1274+
1275+
# non-monotonic, raise KeyError
1276+
s2 = ser.iloc[list(range(5)) + list(range(9, 4, -1))]
1277+
with pytest.raises(KeyError, match=r"^3$"):
1278+
s2.loc[3:11]
1279+
with pytest.raises(KeyError, match=r"^3$"):
1280+
s2.loc[3:11] = 0
1281+
1282+
def test_loc_getitem_iterator(self, string_series):
1283+
idx = iter(string_series.index[:10])
1284+
result = string_series.loc[idx]
1285+
tm.assert_series_equal(result, string_series[:10])
1286+
1287+
def test_loc_setitem_boolean(self, string_series):
1288+
mask = string_series > string_series.median()
1289+
1290+
result = string_series.copy()
1291+
result.loc[mask] = 0
1292+
expected = string_series
1293+
expected[mask] = 0
1294+
tm.assert_series_equal(result, expected)
1295+
1296+
def test_loc_setitem_corner(self, string_series):
1297+
inds = list(string_series.index[[5, 8, 12]])
1298+
string_series.loc[inds] = 5
1299+
msg = r"\['foo'\] not in index"
1300+
with pytest.raises(KeyError, match=msg):
1301+
string_series.loc[inds + ["foo"]] = 5
1302+
1303+
def test_basic_setitem_with_labels(self, datetime_series):
1304+
indices = datetime_series.index[[5, 10, 15]]
1305+
1306+
cp = datetime_series.copy()
1307+
exp = datetime_series.copy()
1308+
cp[indices] = 0
1309+
exp.loc[indices] = 0
1310+
tm.assert_series_equal(cp, exp)
1311+
1312+
cp = datetime_series.copy()
1313+
exp = datetime_series.copy()
1314+
cp[indices[0] : indices[2]] = 0
1315+
exp.loc[indices[0] : indices[2]] = 0
1316+
tm.assert_series_equal(cp, exp)
1317+
1318+
def test_loc_setitem_listlike_of_ints(self):
1319+
1320+
# integer indexes, be careful
1321+
ser = Series(np.random.randn(10), index=list(range(0, 20, 2)))
1322+
inds = [0, 4, 6]
1323+
arr_inds = np.array([0, 4, 6])
1324+
1325+
cp = ser.copy()
1326+
exp = ser.copy()
1327+
ser[inds] = 0
1328+
ser.loc[inds] = 0
1329+
tm.assert_series_equal(cp, exp)
1330+
1331+
cp = ser.copy()
1332+
exp = ser.copy()
1333+
ser[arr_inds] = 0
1334+
ser.loc[arr_inds] = 0
1335+
tm.assert_series_equal(cp, exp)
1336+
1337+
inds_notfound = [0, 4, 5, 6]
1338+
arr_inds_notfound = np.array([0, 4, 5, 6])
1339+
msg = r"\[5\] not in index"
1340+
with pytest.raises(KeyError, match=msg):
1341+
ser[inds_notfound] = 0
1342+
with pytest.raises(Exception, match=msg):
1343+
ser[arr_inds_notfound] = 0
1344+
1345+
def test_loc_setitem_dt64tz_values(self):
1346+
# GH#12089
1347+
ser = Series(
1348+
date_range("2011-01-01", periods=3, tz="US/Eastern"),
1349+
index=["a", "b", "c"],
1350+
)
1351+
s2 = ser.copy()
1352+
expected = Timestamp("2011-01-03", tz="US/Eastern")
1353+
s2.loc["a"] = expected
1354+
result = s2.loc["a"]
1355+
assert result == expected
1356+
1357+
s2 = ser.copy()
1358+
s2.iloc[0] = expected
1359+
result = s2.iloc[0]
1360+
assert result == expected
1361+
1362+
s2 = ser.copy()
1363+
s2["a"] = expected
1364+
result = s2["a"]
1365+
assert result == expected

pandas/tests/series/indexing/test_iloc.py

-32
This file was deleted.

pandas/tests/series/indexing/test_loc.py

-159
This file was deleted.

0 commit comments

Comments
 (0)