Skip to content

Commit b362d5f

Browse files
jbrockmendelukarroum
authored andcommitted
TST/REF: collect indexing tests by method (pandas-dev#37507)
* TST/REF: indexing tests by method * TST: collect indexing tests by method
1 parent 290ad51 commit b362d5f

File tree

7 files changed

+170
-158
lines changed

7 files changed

+170
-158
lines changed

pandas/tests/frame/methods/test_reset_index.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import pandas as pd
1010
from pandas import (
11+
Categorical,
12+
CategoricalIndex,
1113
DataFrame,
1214
Index,
1315
IntervalIndex,
@@ -542,6 +544,33 @@ def test_reset_index_nat_multiindex(self, ix_data, exp_data):
542544
expected = DataFrame(exp_data)
543545
tm.assert_frame_equal(result, expected)
544546

547+
@pytest.mark.parametrize(
548+
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
549+
)
550+
def test_rest_index_multiindex_categorical_with_missing_values(self, codes):
551+
# GH#24206
552+
553+
index = MultiIndex(
554+
[CategoricalIndex(["A", "B"]), CategoricalIndex(["a", "b"])], codes
555+
)
556+
data = {"col": range(len(index))}
557+
df = DataFrame(data=data, index=index)
558+
559+
expected = DataFrame(
560+
{
561+
"level_0": Categorical.from_codes(codes[0], categories=["A", "B"]),
562+
"level_1": Categorical.from_codes(codes[1], categories=["a", "b"]),
563+
"col": range(4),
564+
}
565+
)
566+
567+
res = df.reset_index()
568+
tm.assert_frame_equal(res, expected)
569+
570+
# roundtrip
571+
res = expected.set_index(["level_0", "level_1"]).reset_index()
572+
tm.assert_frame_equal(res, expected)
573+
545574

546575
@pytest.mark.parametrize(
547576
"array, dtype",

pandas/tests/frame/test_alter_axes.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
)
1111

1212
from pandas import (
13-
Categorical,
14-
CategoricalIndex,
1513
DataFrame,
1614
DatetimeIndex,
1715
Index,
1816
IntervalIndex,
19-
MultiIndex,
2017
Series,
2118
Timestamp,
2219
cut,
@@ -192,32 +189,3 @@ def test_set_reset_index(self):
192189
df = df.set_index("B")
193190

194191
df = df.reset_index()
195-
196-
197-
class TestCategoricalIndex:
198-
@pytest.mark.parametrize(
199-
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
200-
)
201-
def test_reindexing_with_missing_values(self, codes):
202-
# GH 24206
203-
204-
index = MultiIndex(
205-
[CategoricalIndex(["A", "B"]), CategoricalIndex(["a", "b"])], codes
206-
)
207-
data = {"col": range(len(index))}
208-
df = DataFrame(data=data, index=index)
209-
210-
expected = DataFrame(
211-
{
212-
"level_0": Categorical.from_codes(codes[0], categories=["A", "B"]),
213-
"level_1": Categorical.from_codes(codes[1], categories=["a", "b"]),
214-
"col": range(4),
215-
}
216-
)
217-
218-
res = df.reset_index()
219-
tm.assert_frame_equal(res, expected)
220-
221-
# roundtrip
222-
res = expected.set_index(["level_0", "level_1"]).reset_index()
223-
tm.assert_frame_equal(res, expected)

pandas/tests/series/indexing/test_getitem.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@
99
from pandas._libs.tslibs import conversion, timezones
1010

1111
import pandas as pd
12-
from pandas import Index, Series, Timestamp, date_range, period_range
12+
from pandas import DataFrame, Index, Series, Timestamp, date_range, period_range
1313
import pandas._testing as tm
1414
from pandas.core.indexing import IndexingError
1515

1616
from pandas.tseries.offsets import BDay
1717

1818

1919
class TestSeriesGetitemScalars:
20+
def test_getitem_out_of_bounds_indexerror(self, datetime_series):
21+
# don't segfault, GH#495
22+
msg = r"index \d+ is out of bounds for axis 0 with size \d+"
23+
with pytest.raises(IndexError, match=msg):
24+
datetime_series[len(datetime_series)]
25+
26+
def test_getitem_out_of_bounds_empty_rangeindex_keyerror(self):
27+
# GH#917
28+
# With a RangeIndex, an int key gives a KeyError
29+
ser = Series([], dtype=object)
30+
with pytest.raises(KeyError, match="-1"):
31+
ser[-1]
32+
2033
def test_getitem_keyerror_with_int64index(self):
2134
ser = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])
2235

@@ -292,11 +305,23 @@ def test_getitem_multilevel_scalar_slice_not_implemented(
292305
ser[2000, 3:4]
293306

294307

308+
def test_getitem_dataframe_raises():
309+
rng = list(range(10))
310+
ser = Series(10, index=rng)
311+
df = DataFrame(rng, index=rng)
312+
msg = (
313+
"Indexing a Series with DataFrame is not supported, "
314+
"use the appropriate DataFrame column"
315+
)
316+
with pytest.raises(TypeError, match=msg):
317+
ser[df > 5]
318+
319+
295320
def test_getitem_assignment_series_aligment():
296321
# https://github.com/pandas-dev/pandas/issues/37427
297322
# with getitem, when assigning with a Series, it is not first aligned
298-
s = Series(range(10))
323+
ser = Series(range(10))
299324
idx = np.array([2, 4, 9])
300-
s[idx] = Series([10, 11, 12])
325+
ser[idx] = Series([10, 11, 12])
301326
expected = Series([0, 1, 10, 3, 11, 5, 6, 7, 8, 12])
302-
tm.assert_series_equal(s, expected)
327+
tm.assert_series_equal(ser, expected)

pandas/tests/series/indexing/test_indexing.py

Lines changed: 1 addition & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,6 @@ def test_getitem_with_duplicates_indices(result_1, duplicate_item, expected_1):
163163
assert result[2] == result_1[2]
164164

165165

166-
def test_getitem_out_of_bounds(datetime_series):
167-
# don't segfault, GH #495
168-
msg = r"index \d+ is out of bounds for axis 0 with size \d+"
169-
with pytest.raises(IndexError, match=msg):
170-
datetime_series[len(datetime_series)]
171-
172-
# GH #917
173-
# With a RangeIndex, an int key gives a KeyError
174-
s = Series([], dtype=object)
175-
with pytest.raises(KeyError, match="-1"):
176-
s[-1]
177-
178-
179166
def test_getitem_setitem_integers():
180167
# caused bug without test
181168
s = Series([1, 2, 3], ["a", "b", "c"])
@@ -260,18 +247,6 @@ def test_setitem_ambiguous_keyerror():
260247
tm.assert_series_equal(s2, expected)
261248

262249

263-
def test_getitem_dataframe():
264-
rng = list(range(10))
265-
s = Series(10, index=rng)
266-
df = DataFrame(rng, index=rng)
267-
msg = (
268-
"Indexing a Series with DataFrame is not supported, "
269-
"use the appropriate DataFrame column"
270-
)
271-
with pytest.raises(TypeError, match=msg):
272-
s[df > 5]
273-
274-
275250
def test_setitem(datetime_series, string_series):
276251
datetime_series[datetime_series.index[5]] = np.NaN
277252
datetime_series[[1, 2, 17]] = np.NaN
@@ -296,22 +271,6 @@ def test_setitem(datetime_series, string_series):
296271
tm.assert_series_equal(s, expected)
297272

298273

299-
def test_setitem_empty_series():
300-
# Test for issue #10193
301-
key = pd.Timestamp("2012-01-01")
302-
series = Series(dtype=object)
303-
series[key] = 47
304-
expected = Series(47, [key])
305-
tm.assert_series_equal(series, expected)
306-
307-
# GH#33573 our index should retain its freq
308-
series = Series([], pd.DatetimeIndex([], freq="D"), dtype=object)
309-
series[key] = 47
310-
expected = Series(47, pd.DatetimeIndex([key], freq="D"))
311-
tm.assert_series_equal(series, expected)
312-
assert series.index.freq == expected.index.freq
313-
314-
315274
def test_setitem_dtypes():
316275
# change dtypes
317276
# GH 4463
@@ -338,32 +297,13 @@ def test_setitem_dtypes():
338297
tm.assert_series_equal(s, Series([np.nan, 1.0]))
339298

340299

341-
def test_set_value(datetime_series, string_series):
342-
idx = datetime_series.index[10]
343-
res = datetime_series._set_value(idx, 0)
344-
assert res is None
345-
assert datetime_series[idx] == 0
346-
347-
# equiv
348-
s = string_series.copy()
349-
res = s._set_value("foobar", 0)
350-
assert res is None
351-
assert s.index[-1] == "foobar"
352-
assert s["foobar"] == 0
353-
354-
s = string_series.copy()
355-
s.loc["foobar"] = 0
356-
assert s.index[-1] == "foobar"
357-
assert s["foobar"] == 0
358-
359-
360300
def test_setslice(datetime_series):
361301
sl = datetime_series[5:20]
362302
assert len(sl) == len(sl.index)
363303
assert sl.index.is_unique is True
364304

365305

366-
def test_2d_to_1d_assignment_raises():
306+
def test_loc_setitem_2d_to_1d_raises():
367307
x = np.random.randn(2, 2)
368308
y = Series(range(2))
369309

@@ -611,25 +551,6 @@ def test_loc_setitem(string_series):
611551
assert string_series[d2] == 6
612552

613553

614-
def test_setitem_na():
615-
# these induce dtype changes
616-
expected = Series([np.nan, 3, np.nan, 5, np.nan, 7, np.nan, 9, np.nan])
617-
s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10])
618-
s[::2] = np.nan
619-
tm.assert_series_equal(s, expected)
620-
621-
# gets coerced to float, right?
622-
expected = Series([np.nan, 1, np.nan, 0])
623-
s = Series([True, True, False, False])
624-
s[::2] = np.nan
625-
tm.assert_series_equal(s, expected)
626-
627-
expected = Series([np.nan, np.nan, np.nan, np.nan, np.nan, 5, 6, 7, 8, 9])
628-
s = Series(np.arange(10))
629-
s[:5] = np.nan
630-
tm.assert_series_equal(s, expected)
631-
632-
633554
def test_timedelta_assignment():
634555
# GH 8209
635556
s = Series([], dtype=object)
@@ -829,52 +750,11 @@ def test_multilevel_preserve_name():
829750
assert result2.name == s.name
830751

831752

832-
def test_setitem_scalar_into_readonly_backing_data():
833-
# GH14359: test that you cannot mutate a read only buffer
834-
835-
array = np.zeros(5)
836-
array.flags.writeable = False # make the array immutable
837-
series = Series(array)
838-
839-
for n in range(len(series)):
840-
msg = "assignment destination is read-only"
841-
with pytest.raises(ValueError, match=msg):
842-
series[n] = 1
843-
844-
assert array[n] == 0
845-
846-
847-
def test_setitem_slice_into_readonly_backing_data():
848-
# GH14359: test that you cannot mutate a read only buffer
849-
850-
array = np.zeros(5)
851-
array.flags.writeable = False # make the array immutable
852-
series = Series(array)
853-
854-
msg = "assignment destination is read-only"
855-
with pytest.raises(ValueError, match=msg):
856-
series[1:3] = 1
857-
858-
assert not array.any()
859-
860-
861753
"""
862754
miscellaneous methods
863755
"""
864756

865757

866-
def test_pop():
867-
# GH 6600
868-
df = DataFrame({"A": 0, "B": np.arange(5, dtype="int64"), "C": 0})
869-
k = df.iloc[4]
870-
871-
result = k.pop("B")
872-
assert result == 4
873-
874-
expected = Series([0, 0], index=["A", "C"], name=4)
875-
tm.assert_series_equal(k, expected)
876-
877-
878758
def test_uint_drop(any_int_dtype):
879759
# see GH18311
880760
# assigning series.loc[0] = 4 changed series.dtype to int
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pandas import Series
2+
import pandas._testing as tm
3+
4+
5+
def test_pop():
6+
# GH#6600
7+
ser = Series([0, 4, 0], index=["A", "B", "C"], name=4)
8+
9+
result = ser.pop("B")
10+
assert result == 4
11+
12+
expected = Series([0, 0], index=["A", "C"], name=4)
13+
tm.assert_series_equal(ser, expected)

pandas/tests/series/indexing/test_set_value.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,24 @@ def test_series_set_value():
1919
expected = Series([1.0, np.nan], index=index)
2020

2121
tm.assert_series_equal(s, expected)
22+
23+
24+
def test_set_value_dt64(datetime_series):
25+
idx = datetime_series.index[10]
26+
res = datetime_series._set_value(idx, 0)
27+
assert res is None
28+
assert datetime_series[idx] == 0
29+
30+
31+
def test_set_value_str_index(string_series):
32+
# equiv
33+
ser = string_series.copy()
34+
res = ser._set_value("foobar", 0)
35+
assert res is None
36+
assert ser.index[-1] == "foobar"
37+
assert ser["foobar"] == 0
38+
39+
ser2 = string_series.copy()
40+
ser2.loc["foobar"] = 0
41+
assert ser2.index[-1] == "foobar"
42+
assert ser2["foobar"] == 0

0 commit comments

Comments
 (0)