Skip to content

Commit 255267f

Browse files
committed
Fix more indexing tests
1 parent 74e09e4 commit 255267f

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

pandas/tests/frame/indexing/test_getitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_getitem_list_duplicates(self):
106106

107107
def test_getitem_dupe_cols(self):
108108
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
109-
msg = "\"None of [Index(['baf'], dtype='object')] are in the [columns]\""
109+
msg = "\"None of [Index(['baf'], dtype=.*)] are in the [columns]\""
110110
with pytest.raises(KeyError, match=re.escape(msg)):
111111
df[["baf"]]
112112

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_setattr_column(self):
288288
df.foobar = 5
289289
assert (df.foobar == 5).all()
290290

291-
def test_setitem(self, float_frame, using_copy_on_write):
291+
def test_setitem(self, float_frame, using_copy_on_write, using_infer_string):
292292
# not sure what else to do here
293293
series = float_frame["A"][::2]
294294
float_frame["col5"] = series
@@ -331,7 +331,10 @@ def test_setitem(self, float_frame, using_copy_on_write):
331331
with pytest.raises(SettingWithCopyError, match=msg):
332332
smaller["col10"] = ["1", "2"]
333333

334-
assert smaller["col10"].dtype == np.object_
334+
if using_infer_string:
335+
assert smaller["col10"].dtype == "string"
336+
else:
337+
assert smaller["col10"].dtype == np.object_
335338
assert (smaller["col10"] == ["1", "2"]).all()
336339

337340
def test_setitem2(self):
@@ -426,7 +429,7 @@ def test_setitem_cast(self, float_frame):
426429
float_frame["something"] = 2.5
427430
assert float_frame["something"].dtype == np.float64
428431

429-
def test_setitem_corner(self, float_frame):
432+
def test_setitem_corner(self, float_frame, using_infer_string):
430433
# corner case
431434
df = DataFrame({"B": [1.0, 2.0, 3.0], "C": ["a", "b", "c"]}, index=np.arange(3))
432435
del df["B"]
@@ -463,10 +466,16 @@ def test_setitem_corner(self, float_frame):
463466
dm["foo"] = "bar"
464467
del dm["foo"]
465468
dm["foo"] = "bar"
466-
assert dm["foo"].dtype == np.object_
469+
if using_infer_string:
470+
assert dm["foo"].dtype == "string"
471+
else:
472+
assert dm["foo"].dtype == np.object_
467473

468474
dm["coercible"] = ["1", "2", "3"]
469-
assert dm["coercible"].dtype == np.object_
475+
if using_infer_string:
476+
assert dm["coercible"].dtype == "string"
477+
else:
478+
assert dm["coercible"].dtype == np.object_
470479

471480
def test_setitem_corner2(self):
472481
data = {
@@ -483,7 +492,7 @@ def test_setitem_corner2(self):
483492
assert df.loc[1, "title"] == "foobar"
484493
assert df.loc[1, "cruft"] == 0
485494

486-
def test_setitem_ambig(self):
495+
def test_setitem_ambig(self, using_infer_string):
487496
# Difficulties with mixed-type data
488497
# Created as float type
489498
dm = DataFrame(index=range(3), columns=range(3))
@@ -499,18 +508,22 @@ def test_setitem_ambig(self):
499508

500509
dm[2] = uncoercable_series
501510
assert len(dm.columns) == 3
502-
assert dm[2].dtype == np.object_
511+
if using_infer_string:
512+
assert dm[2].dtype == "string"
513+
else:
514+
assert dm[2].dtype == np.object_
503515

504-
def test_setitem_None(self, float_frame):
516+
def test_setitem_None(self, float_frame, using_infer_string):
505517
# GH #766
506518
float_frame[None] = float_frame["A"]
519+
key = None if not using_infer_string else np.nan
507520
tm.assert_series_equal(
508521
float_frame.iloc[:, -1], float_frame["A"], check_names=False
509522
)
510523
tm.assert_series_equal(
511-
float_frame.loc[:, None], float_frame["A"], check_names=False
524+
float_frame.loc[:, key], float_frame["A"], check_names=False
512525
)
513-
tm.assert_series_equal(float_frame[None], float_frame["A"], check_names=False)
526+
tm.assert_series_equal(float_frame[key], float_frame["A"], check_names=False)
514527
repr(float_frame)
515528

516529
def test_loc_setitem_boolean_mask_allfalse(self):

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ def test_setitem_column_frame_as_category(self):
12981298
df["col2"] = Series([1, 2, 3], dtype="category")
12991299

13001300
expected_types = Series(
1301-
["int64", "category", "category"], index=[0, "col1", "col2"]
1301+
["int64", "category", "category"], index=[0, "col1", "col2"], dtype=object
13021302
)
13031303
tm.assert_series_equal(df.dtypes, expected_types)
13041304

0 commit comments

Comments
 (0)