Skip to content

Commit 5c4f737

Browse files
authored
TST/REF: collect indexing tests by method (#37729)
1 parent dcaa5c2 commit 5c4f737

File tree

10 files changed

+238
-271
lines changed

10 files changed

+238
-271
lines changed

pandas/tests/frame/indexing/test_getitem.py

+17
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
CategoricalIndex,
88
DataFrame,
99
MultiIndex,
10+
Series,
1011
Timestamp,
1112
get_dummies,
1213
period_range,
1314
)
1415
import pandas._testing as tm
16+
from pandas.core.arrays import SparseArray
1517

1618

1719
class TestGetitem:
@@ -50,6 +52,21 @@ def test_getitem_list_of_labels_categoricalindex_cols(self):
5052
result = dummies[list(dummies.columns)]
5153
tm.assert_frame_equal(result, expected)
5254

55+
def test_getitem_sparse_column_return_type_and_dtype(self):
56+
# https://github.com/pandas-dev/pandas/issues/23559
57+
data = SparseArray([0, 1])
58+
df = DataFrame({"A": data})
59+
expected = Series(data, name="A")
60+
result = df["A"]
61+
tm.assert_series_equal(result, expected)
62+
63+
# Also check iloc and loc while we're here
64+
result = df.iloc[:, 0]
65+
tm.assert_series_equal(result, expected)
66+
67+
result = df.loc[:, "A"]
68+
tm.assert_series_equal(result, expected)
69+
5370

5471
class TestGetitemCallable:
5572
def test_getitem_callable(self, float_frame):

pandas/tests/frame/indexing/test_sparse.py

-19
This file was deleted.

pandas/tests/indexing/test_at.py

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def test_at_timezone():
1717
tm.assert_frame_equal(result, expected)
1818

1919

20+
class TestAtSetItem:
21+
def test_at_setitem_mixed_index_assignment(self):
22+
# GH#19860
23+
ser = Series([1, 2, 3, 4, 5], index=["a", "b", "c", 1, 2])
24+
ser.at["a"] = 11
25+
assert ser.iat[0] == 11
26+
ser.at[1] = 22
27+
assert ser.iat[3] == 22
28+
29+
2030
class TestAtWithDuplicates:
2131
def test_at_with_duplicate_axes_requires_scalar_lookup(self):
2232
# GH#33041 check that falling back to loc doesn't allow non-scalar
@@ -108,3 +118,11 @@ def test_at_frame_raises_key_error2(self):
108118
df.at["a", 0]
109119
with pytest.raises(KeyError, match="^0$"):
110120
df.loc["a", 0]
121+
122+
def test_at_getitem_mixed_index_no_fallback(self):
123+
# GH#19860
124+
ser = Series([1, 2, 3, 4, 5], index=["a", "b", "c", 1, 2])
125+
with pytest.raises(KeyError, match="^0$"):
126+
ser.at[0]
127+
with pytest.raises(KeyError, match="^4$"):
128+
ser.at[4]

pandas/tests/indexing/test_floats.py

+11-64
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import re
2-
31
import numpy as np
42
import pytest
53

64
from pandas import DataFrame, Float64Index, Index, Int64Index, RangeIndex, Series
75
import pandas._testing as tm
86

9-
# We pass through the error message from numpy
10-
_slice_iloc_msg = re.escape(
11-
"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) "
12-
"and integer or boolean arrays are valid indices"
13-
)
14-
157

168
def gen_obj(klass, index):
179
if klass is Series:
@@ -40,24 +32,6 @@ def check(self, result, original, indexer, getitem):
4032

4133
tm.assert_almost_equal(result, expected)
4234

43-
def test_scalar_error(self, series_with_simple_index):
44-
45-
# GH 4892
46-
# float_indexers should raise exceptions
47-
# on appropriate Index types & accessors
48-
# this duplicates the code below
49-
# but is specifically testing for the error
50-
# message
51-
52-
s = series_with_simple_index
53-
54-
msg = "Cannot index by location index with a non-integer key"
55-
with pytest.raises(TypeError, match=msg):
56-
s.iloc[3.0]
57-
58-
with pytest.raises(IndexError, match=_slice_iloc_msg):
59-
s.iloc[3.0] = 0
60-
6135
@pytest.mark.parametrize(
6236
"index_func",
6337
[
@@ -69,40 +43,32 @@ def test_scalar_error(self, series_with_simple_index):
6943
tm.makePeriodIndex,
7044
],
7145
)
72-
@pytest.mark.parametrize("klass", [Series, DataFrame])
73-
def test_scalar_non_numeric(self, index_func, klass):
46+
def test_scalar_non_numeric(self, index_func, frame_or_series):
7447

7548
# GH 4892
7649
# float_indexers should raise exceptions
7750
# on appropriate Index types & accessors
7851

7952
i = index_func(5)
80-
s = gen_obj(klass, i)
53+
s = gen_obj(frame_or_series, i)
8154

8255
# getting
8356
with pytest.raises(KeyError, match="^3.0$"):
8457
s[3.0]
8558

86-
msg = "Cannot index by location index with a non-integer key"
87-
with pytest.raises(TypeError, match=msg):
88-
s.iloc[3.0]
89-
9059
with pytest.raises(KeyError, match="^3.0$"):
9160
s.loc[3.0]
9261

9362
# contains
9463
assert 3.0 not in s
9564

96-
# setting with a float fails with iloc
97-
with pytest.raises(IndexError, match=_slice_iloc_msg):
98-
s.iloc[3.0] = 0
99-
10065
# setting with an indexer
10166
if s.index.inferred_type in ["categorical"]:
10267
# Value or Type Error
10368
pass
10469
elif s.index.inferred_type in ["datetime64", "timedelta64", "period"]:
10570

71+
# FIXME: dont leave commented-out
10672
# these should prob work
10773
# and are inconsistent between series/dataframe ATM
10874
# for idxr in [lambda x: x]:
@@ -151,10 +117,6 @@ def test_scalar_with_mixed(self):
151117
with pytest.raises(KeyError, match="^1.0$"):
152118
s2[1.0]
153119

154-
msg = "Cannot index by location index with a non-integer key"
155-
with pytest.raises(TypeError, match=msg):
156-
s2.iloc[1.0]
157-
158120
with pytest.raises(KeyError, match=r"^1\.0$"):
159121
s2.loc[1.0]
160122

@@ -171,9 +133,6 @@ def test_scalar_with_mixed(self):
171133
expected = 2
172134
assert result == expected
173135

174-
msg = "Cannot index by location index with a non-integer key"
175-
with pytest.raises(TypeError, match=msg):
176-
s3.iloc[1.0]
177136
with pytest.raises(KeyError, match=r"^1\.0$"):
178137
s3.loc[1.0]
179138

@@ -182,14 +141,13 @@ def test_scalar_with_mixed(self):
182141
assert result == expected
183142

184143
@pytest.mark.parametrize("index_func", [tm.makeIntIndex, tm.makeRangeIndex])
185-
@pytest.mark.parametrize("klass", [Series, DataFrame])
186-
def test_scalar_integer(self, index_func, klass):
144+
def test_scalar_integer(self, index_func, frame_or_series):
187145

188146
# test how scalar float indexers work on int indexes
189147

190148
# integer index
191149
i = index_func(5)
192-
obj = gen_obj(klass, i)
150+
obj = gen_obj(frame_or_series, i)
193151

194152
# coerce to equal int
195153
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:
@@ -226,12 +184,11 @@ def compare(x, y):
226184
# coerce to equal int
227185
assert 3.0 in obj
228186

229-
@pytest.mark.parametrize("klass", [Series, DataFrame])
230-
def test_scalar_float(self, klass):
187+
def test_scalar_float(self, frame_or_series):
231188

232189
# scalar float indexers work on a float index
233190
index = Index(np.arange(5.0))
234-
s = gen_obj(klass, index)
191+
s = gen_obj(frame_or_series, index)
235192

236193
# assert all operations except for iloc are ok
237194
indexer = index[3]
@@ -262,14 +219,6 @@ def test_scalar_float(self, klass):
262219
result = s2.iloc[3]
263220
self.check(result, s, 3, False)
264221

265-
# iloc raises with a float
266-
msg = "Cannot index by location index with a non-integer key"
267-
with pytest.raises(TypeError, match=msg):
268-
s.iloc[3.0]
269-
270-
with pytest.raises(IndexError, match=_slice_iloc_msg):
271-
s2.iloc[3.0] = 0
272-
273222
@pytest.mark.parametrize(
274223
"index_func",
275224
[
@@ -281,15 +230,14 @@ def test_scalar_float(self, klass):
281230
],
282231
)
283232
@pytest.mark.parametrize("l", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
284-
@pytest.mark.parametrize("klass", [Series, DataFrame])
285-
def test_slice_non_numeric(self, index_func, l, klass):
233+
def test_slice_non_numeric(self, index_func, l, frame_or_series):
286234

287235
# GH 4892
288236
# float_indexers should raise exceptions
289237
# on appropriate Index types & accessors
290238

291239
index = index_func(5)
292-
s = gen_obj(klass, index)
240+
s = gen_obj(frame_or_series, index)
293241

294242
# getitem
295243
msg = (
@@ -509,12 +457,11 @@ def test_float_slice_getitem_with_integer_index_raises(self, l, index_func):
509457
s[l]
510458

511459
@pytest.mark.parametrize("l", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
512-
@pytest.mark.parametrize("klass", [Series, DataFrame])
513-
def test_slice_float(self, l, klass):
460+
def test_slice_float(self, l, frame_or_series):
514461

515462
# same as above, but for floats
516463
index = Index(np.arange(5.0)) + 0.1
517-
s = gen_obj(klass, index)
464+
s = gen_obj(frame_or_series, index)
518465

519466
expected = s.iloc[3:4]
520467
for idxr in [lambda x: x.loc, lambda x: x]:

0 commit comments

Comments
 (0)