Skip to content

Commit 3fb865e

Browse files
simonjayhawkinsjreback
authored andcommitted
DEPR: remove .ix from tests/indexing/multiindex/test_setitem.py (#27574)
1 parent ba31de3 commit 3fb865e

File tree

1 file changed

+118
-150
lines changed

1 file changed

+118
-150
lines changed

pandas/tests/indexing/multiindex/test_setitem.py

+118-150
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from warnings import catch_warnings, simplefilter
2-
31
import numpy as np
42
from numpy.random import randn
53
import pytest
@@ -10,133 +8,114 @@
108
from pandas.util import testing as tm
119

1210

13-
@pytest.mark.filterwarnings("ignore:\\n.ix:FutureWarning")
1411
class TestMultiIndexSetItem:
1512
def test_setitem_multiindex(self):
16-
with catch_warnings(record=True):
17-
18-
for index_fn in ("ix", "loc"):
19-
20-
def assert_equal(a, b):
21-
assert a == b
22-
23-
def check(target, indexers, value, compare_fn, expected=None):
24-
fn = getattr(target, index_fn)
25-
fn.__setitem__(indexers, value)
26-
result = fn.__getitem__(indexers)
27-
if expected is None:
28-
expected = value
29-
compare_fn(result, expected)
30-
31-
# GH7190
32-
index = MultiIndex.from_product(
33-
[np.arange(0, 100), np.arange(0, 80)], names=["time", "firm"]
34-
)
35-
t, n = 0, 2
36-
df = DataFrame(
37-
np.nan,
38-
columns=["A", "w", "l", "a", "x", "X", "d", "profit"],
39-
index=index,
40-
)
41-
check(
42-
target=df, indexers=((t, n), "X"), value=0, compare_fn=assert_equal
43-
)
44-
45-
df = DataFrame(
46-
-999,
47-
columns=["A", "w", "l", "a", "x", "X", "d", "profit"],
48-
index=index,
49-
)
50-
check(
51-
target=df, indexers=((t, n), "X"), value=1, compare_fn=assert_equal
52-
)
53-
54-
df = DataFrame(
55-
columns=["A", "w", "l", "a", "x", "X", "d", "profit"], index=index
56-
)
57-
check(
58-
target=df, indexers=((t, n), "X"), value=2, compare_fn=assert_equal
59-
)
60-
61-
# gh-7218: assigning with 0-dim arrays
62-
df = DataFrame(
63-
-999,
64-
columns=["A", "w", "l", "a", "x", "X", "d", "profit"],
65-
index=index,
66-
)
67-
check(
68-
target=df,
69-
indexers=((t, n), "X"),
70-
value=np.array(3),
71-
compare_fn=assert_equal,
72-
expected=3,
73-
)
74-
75-
# GH5206
76-
df = DataFrame(
77-
np.arange(25).reshape(5, 5),
78-
columns="A,B,C,D,E".split(","),
79-
dtype=float,
80-
)
81-
df["F"] = 99
82-
row_selection = df["A"] % 2 == 0
83-
col_selection = ["B", "C"]
84-
with catch_warnings(record=True):
85-
df.ix[row_selection, col_selection] = df["F"]
86-
output = DataFrame(99.0, index=[0, 2, 4], columns=["B", "C"])
87-
with catch_warnings(record=True):
88-
tm.assert_frame_equal(df.ix[row_selection, col_selection], output)
89-
check(
90-
target=df,
91-
indexers=(row_selection, col_selection),
92-
value=df["F"],
93-
compare_fn=tm.assert_frame_equal,
94-
expected=output,
95-
)
96-
97-
# GH11372
98-
idx = MultiIndex.from_product(
99-
[["A", "B", "C"], date_range("2015-01-01", "2015-04-01", freq="MS")]
100-
)
101-
cols = MultiIndex.from_product(
102-
[["foo", "bar"], date_range("2016-01-01", "2016-02-01", freq="MS")]
103-
)
104-
105-
df = DataFrame(np.random.random((12, 4)), index=idx, columns=cols)
106-
107-
subidx = MultiIndex.from_tuples(
108-
[("A", Timestamp("2015-01-01")), ("A", Timestamp("2015-02-01"))]
109-
)
110-
subcols = MultiIndex.from_tuples(
111-
[("foo", Timestamp("2016-01-01")), ("foo", Timestamp("2016-02-01"))]
112-
)
113-
114-
vals = DataFrame(
115-
np.random.random((2, 2)), index=subidx, columns=subcols
116-
)
117-
check(
118-
target=df,
119-
indexers=(subidx, subcols),
120-
value=vals,
121-
compare_fn=tm.assert_frame_equal,
122-
)
123-
# set all columns
124-
vals = DataFrame(np.random.random((2, 4)), index=subidx, columns=cols)
125-
check(
126-
target=df,
127-
indexers=(subidx, slice(None, None, None)),
128-
value=vals,
129-
compare_fn=tm.assert_frame_equal,
130-
)
131-
# identity
132-
copy = df.copy()
133-
check(
134-
target=df,
135-
indexers=(df.index, df.columns),
136-
value=df,
137-
compare_fn=tm.assert_frame_equal,
138-
expected=copy,
139-
)
13+
for index_fn in ("loc",):
14+
15+
def assert_equal(a, b):
16+
assert a == b
17+
18+
def check(target, indexers, value, compare_fn, expected=None):
19+
fn = getattr(target, index_fn)
20+
fn.__setitem__(indexers, value)
21+
result = fn.__getitem__(indexers)
22+
if expected is None:
23+
expected = value
24+
compare_fn(result, expected)
25+
26+
# GH7190
27+
index = MultiIndex.from_product(
28+
[np.arange(0, 100), np.arange(0, 80)], names=["time", "firm"]
29+
)
30+
t, n = 0, 2
31+
df = DataFrame(
32+
np.nan,
33+
columns=["A", "w", "l", "a", "x", "X", "d", "profit"],
34+
index=index,
35+
)
36+
check(target=df, indexers=((t, n), "X"), value=0, compare_fn=assert_equal)
37+
38+
df = DataFrame(
39+
-999, columns=["A", "w", "l", "a", "x", "X", "d", "profit"], index=index
40+
)
41+
check(target=df, indexers=((t, n), "X"), value=1, compare_fn=assert_equal)
42+
43+
df = DataFrame(
44+
columns=["A", "w", "l", "a", "x", "X", "d", "profit"], index=index
45+
)
46+
check(target=df, indexers=((t, n), "X"), value=2, compare_fn=assert_equal)
47+
48+
# gh-7218: assigning with 0-dim arrays
49+
df = DataFrame(
50+
-999, columns=["A", "w", "l", "a", "x", "X", "d", "profit"], index=index
51+
)
52+
check(
53+
target=df,
54+
indexers=((t, n), "X"),
55+
value=np.array(3),
56+
compare_fn=assert_equal,
57+
expected=3,
58+
)
59+
60+
# GH5206
61+
df = DataFrame(
62+
np.arange(25).reshape(5, 5), columns="A,B,C,D,E".split(","), dtype=float
63+
)
64+
df["F"] = 99
65+
row_selection = df["A"] % 2 == 0
66+
col_selection = ["B", "C"]
67+
df.loc[row_selection, col_selection] = df["F"]
68+
output = DataFrame(99.0, index=[0, 2, 4], columns=["B", "C"])
69+
tm.assert_frame_equal(df.loc[row_selection, col_selection], output)
70+
check(
71+
target=df,
72+
indexers=(row_selection, col_selection),
73+
value=df["F"],
74+
compare_fn=tm.assert_frame_equal,
75+
expected=output,
76+
)
77+
78+
# GH11372
79+
idx = MultiIndex.from_product(
80+
[["A", "B", "C"], date_range("2015-01-01", "2015-04-01", freq="MS")]
81+
)
82+
cols = MultiIndex.from_product(
83+
[["foo", "bar"], date_range("2016-01-01", "2016-02-01", freq="MS")]
84+
)
85+
86+
df = DataFrame(np.random.random((12, 4)), index=idx, columns=cols)
87+
88+
subidx = MultiIndex.from_tuples(
89+
[("A", Timestamp("2015-01-01")), ("A", Timestamp("2015-02-01"))]
90+
)
91+
subcols = MultiIndex.from_tuples(
92+
[("foo", Timestamp("2016-01-01")), ("foo", Timestamp("2016-02-01"))]
93+
)
94+
95+
vals = DataFrame(np.random.random((2, 2)), index=subidx, columns=subcols)
96+
check(
97+
target=df,
98+
indexers=(subidx, subcols),
99+
value=vals,
100+
compare_fn=tm.assert_frame_equal,
101+
)
102+
# set all columns
103+
vals = DataFrame(np.random.random((2, 4)), index=subidx, columns=cols)
104+
check(
105+
target=df,
106+
indexers=(subidx, slice(None, None, None)),
107+
value=vals,
108+
compare_fn=tm.assert_frame_equal,
109+
)
110+
# identity
111+
copy = df.copy()
112+
check(
113+
target=df,
114+
indexers=(df.index, df.columns),
115+
value=df,
116+
compare_fn=tm.assert_frame_equal,
117+
expected=copy,
118+
)
140119

141120
def test_multiindex_setitem(self):
142121

@@ -204,9 +183,8 @@ def test_multiindex_assignment(self):
204183
df["d"] = np.nan
205184
arr = np.array([0.0, 1.0])
206185

207-
with catch_warnings(record=True):
208-
df.ix[4, "d"] = arr
209-
tm.assert_series_equal(df.ix[4, "d"], Series(arr, index=[8, 10], name="d"))
186+
df.loc[4, "d"] = arr
187+
tm.assert_series_equal(df.loc[4, "d"], Series(arr, index=[8, 10], name="d"))
210188

211189
# single dtype
212190
df = DataFrame(
@@ -215,25 +193,21 @@ def test_multiindex_assignment(self):
215193
index=[[4, 4, 8], [8, 10, 12]],
216194
)
217195

218-
with catch_warnings(record=True):
219-
df.ix[4, "c"] = arr
220-
exp = Series(arr, index=[8, 10], name="c", dtype="float64")
221-
tm.assert_series_equal(df.ix[4, "c"], exp)
196+
df.loc[4, "c"] = arr
197+
exp = Series(arr, index=[8, 10], name="c", dtype="float64")
198+
tm.assert_series_equal(df.loc[4, "c"], exp)
222199

223200
# scalar ok
224-
with catch_warnings(record=True):
225-
df.ix[4, "c"] = 10
226-
exp = Series(10, index=[8, 10], name="c", dtype="float64")
227-
tm.assert_series_equal(df.ix[4, "c"], exp)
201+
df.loc[4, "c"] = 10
202+
exp = Series(10, index=[8, 10], name="c", dtype="float64")
203+
tm.assert_series_equal(df.loc[4, "c"], exp)
228204

229205
# invalid assignments
230206
with pytest.raises(ValueError):
231-
with catch_warnings(record=True):
232-
df.ix[4, "c"] = [0, 1, 2, 3]
207+
df.loc[4, "c"] = [0, 1, 2, 3]
233208

234209
with pytest.raises(ValueError):
235-
with catch_warnings(record=True):
236-
df.ix[4, "c"] = [0]
210+
df.loc[4, "c"] = [0]
237211

238212
# groupby example
239213
NUM_ROWS = 100
@@ -264,8 +238,7 @@ def f(name, df2):
264238
# but in this case, that's ok
265239
for name, df2 in grp:
266240
new_vals = np.arange(df2.shape[0])
267-
with catch_warnings(record=True):
268-
df.ix[name, "new_col"] = new_vals
241+
df.loc[name, "new_col"] = new_vals
269242

270243
def test_series_setitem(self, multiindex_year_month_day_dataframe_random_data):
271244
ymd = multiindex_year_month_day_dataframe_random_data
@@ -313,11 +286,6 @@ def test_frame_getitem_setitem_multislice(self):
313286
result = df.loc[:, "value"]
314287
tm.assert_series_equal(df["value"], result)
315288

316-
with catch_warnings(record=True):
317-
simplefilter("ignore", FutureWarning)
318-
result = df.ix[:, "value"]
319-
tm.assert_series_equal(df["value"], result)
320-
321289
result = df.loc[df.index[1:3], "value"]
322290
tm.assert_series_equal(df["value"][1:3], result)
323291

@@ -412,7 +380,7 @@ def test_setitem_change_dtype(self, multiindex_dataframe_random_data):
412380
reindexed = dft.reindex(columns=[("foo", "two")])
413381
tm.assert_series_equal(reindexed["foo", "two"], s > s.median())
414382

415-
def test_set_column_scalar_with_ix(self, multiindex_dataframe_random_data):
383+
def test_set_column_scalar_with_loc(self, multiindex_dataframe_random_data):
416384
frame = multiindex_dataframe_random_data
417385
subset = frame.index[[1, 4, 5]]
418386

0 commit comments

Comments
 (0)