Skip to content

Commit 9bf9423

Browse files
jbrockmendelroberthdevries
authored andcommitted
CLN/TST: parametrize some tests in tests.indexing.test_float (pandas-dev#32187)
* TST: cleanups * troubleshoot * troubleshoot travis
1 parent 41ddc27 commit 9bf9423

File tree

1 file changed

+33
-54
lines changed

1 file changed

+33
-54
lines changed

pandas/tests/indexing/test_floats.py

+33-54
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ def test_scalar_non_numeric(self, index_func, klass):
162162
s2.loc[3.0] = 10
163163
assert s2.index.is_object()
164164

165-
for idxr in [lambda x: x]:
166-
s2 = s.copy()
167-
idxr(s2)[3.0] = 0
168-
assert s2.index.is_object()
165+
s2 = s.copy()
166+
s2[3.0] = 0
167+
assert s2.index.is_object()
169168

170169
@pytest.mark.parametrize(
171170
"index_func",
@@ -250,12 +249,7 @@ def test_scalar_integer(self, index_func, klass):
250249

251250
# integer index
252251
i = index_func(5)
253-
254-
if klass is Series:
255-
# TODO: Should we be passing index=i here?
256-
obj = Series(np.arange(len(i)))
257-
else:
258-
obj = DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i)
252+
obj = gen_obj(klass, i)
259253

260254
# coerce to equal int
261255
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:
@@ -313,7 +307,7 @@ def test_scalar_float(self, klass):
313307
result = idxr(s2)[indexer]
314308
self.check(result, s, 3, getitem)
315309

316-
# random integer is a KeyError
310+
# random float is a KeyError
317311
with pytest.raises(KeyError, match=r"^3\.5$"):
318312
idxr(s)[3.5]
319313

@@ -429,15 +423,6 @@ def test_slice_integer(self):
429423
indexer = slice(3, 5)
430424
self.check(result, s, indexer, False)
431425

432-
# positional indexing
433-
msg = (
434-
"cannot do slice indexing "
435-
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
436-
"type float"
437-
)
438-
with pytest.raises(TypeError, match=msg):
439-
s[l]
440-
441426
# getitem out-of-bounds
442427
for l in [slice(-6, 6), slice(-6.0, 6.0)]:
443428

@@ -485,23 +470,6 @@ def test_slice_integer(self):
485470
with pytest.raises(TypeError, match=msg):
486471
s[l]
487472

488-
# setitem
489-
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:
490-
491-
sc = s.copy()
492-
sc.loc[l] = 0
493-
result = sc.loc[l].values.ravel()
494-
assert (result == 0).all()
495-
496-
# positional indexing
497-
msg = (
498-
"cannot do slice indexing "
499-
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
500-
"type float"
501-
)
502-
with pytest.raises(TypeError, match=msg):
503-
s[l] = 0
504-
505473
@pytest.mark.parametrize("l", [slice(2, 4.0), slice(2.0, 4), slice(2.0, 4.0)])
506474
def test_integer_positional_indexing(self, l):
507475
""" make sure that we are raising on positional indexing
@@ -584,22 +552,34 @@ def test_slice_integer_frame_getitem(self, index_func):
584552
with pytest.raises(TypeError, match=msg):
585553
s[l]
586554

555+
@pytest.mark.parametrize("l", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
556+
@pytest.mark.parametrize(
557+
"index_func", [tm.makeIntIndex, tm.makeRangeIndex],
558+
)
559+
def test_float_slice_getitem_with_integer_index_raises(self, l, index_func):
560+
561+
# similar to above, but on the getitem dim (of a DataFrame)
562+
index = index_func(5)
563+
564+
s = DataFrame(np.random.randn(5, 2), index=index)
565+
587566
# setitem
588-
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:
567+
sc = s.copy()
568+
sc.loc[l] = 0
569+
result = sc.loc[l].values.ravel()
570+
assert (result == 0).all()
589571

590-
sc = s.copy()
591-
sc.loc[l] = 0
592-
result = sc.loc[l].values.ravel()
593-
assert (result == 0).all()
572+
# positional indexing
573+
msg = (
574+
"cannot do slice indexing "
575+
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
576+
"type float"
577+
)
578+
with pytest.raises(TypeError, match=msg):
579+
s[l] = 0
594580

595-
# positional indexing
596-
msg = (
597-
"cannot do slice indexing "
598-
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
599-
"type float"
600-
)
601-
with pytest.raises(TypeError, match=msg):
602-
s[l] = 0
581+
with pytest.raises(TypeError, match=msg):
582+
s[l]
603583

604584
@pytest.mark.parametrize("l", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
605585
@pytest.mark.parametrize("klass", [Series, DataFrame])
@@ -614,10 +594,9 @@ def test_slice_float(self, l, klass):
614594

615595
# getitem
616596
result = idxr(s)[l]
617-
if isinstance(s, Series):
618-
tm.assert_series_equal(result, expected)
619-
else:
620-
tm.assert_frame_equal(result, expected)
597+
assert isinstance(result, type(s))
598+
tm.assert_equal(result, expected)
599+
621600
# setitem
622601
s2 = s.copy()
623602
idxr(s2)[l] = 0

0 commit comments

Comments
 (0)