Skip to content

Commit cf3c44e

Browse files
committed
Update
1 parent 2f5221f commit cf3c44e

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

pandas/tests/indexing/test_indexing.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -531,49 +531,38 @@ def test_astype_assignment(self, using_infer_string):
531531
df_orig = DataFrame(
532532
[["1", "2", "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
533533
)
534+
df_orig[list("ABCDG")] = df_orig[list("ABCDG")].astype(object)
534535

535536
df = df_orig.copy()
536537

537538
# with the enforcement of GH#45333 in 2.0, this setting is attempted inplace,
538539
# so object dtype is retained
539-
if using_infer_string:
540-
with pytest.raises(TypeError, match="Invalid value"):
541-
df.iloc[:, 0] = df.iloc[:, 0].astype(np.int64)
542-
else:
543-
df.iloc[:, 0:2] = df.iloc[:, 0:2].astype(np.int64)
544-
expected = DataFrame(
545-
[[1, 2, "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
546-
)
547-
expected["A"] = expected["A"].astype(object)
548-
expected["B"] = expected["B"].astype(object)
549-
tm.assert_frame_equal(df, expected)
540+
df.iloc[:, 0:2] = df.iloc[:, 0:2].astype(np.int64)
541+
expected = DataFrame(
542+
[[1, 2, "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
543+
)
544+
expected[list("CDG")] = expected[list("CDG")].astype(object)
545+
expected["A"] = expected["A"].astype(object)
546+
expected["B"] = expected["B"].astype(object)
547+
tm.assert_frame_equal(df, expected)
550548

551549
# GH5702 (loc)
552550
df = df_orig.copy()
553-
if using_infer_string:
554-
with pytest.raises(TypeError, match="Invalid value"):
555-
df.loc[:, "A"] = df.loc[:, "A"].astype(np.int64)
556-
else:
557-
df.loc[:, "A"] = df.loc[:, "A"].astype(np.int64)
558-
expected = DataFrame(
559-
[[1, "2", "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
560-
)
561-
expected["A"] = expected["A"].astype(object)
562-
tm.assert_frame_equal(df, expected)
551+
df.loc[:, "A"] = df.loc[:, "A"].astype(np.int64)
552+
expected = DataFrame(
553+
[[1, "2", "3", ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
554+
)
555+
expected[list("ABCDG")] = expected[list("ABCDG")].astype(object)
556+
tm.assert_frame_equal(df, expected)
563557

564558
df = df_orig.copy()
565559

566-
if using_infer_string:
567-
with pytest.raises(TypeError, match="Invalid value"):
568-
df.loc[:, ["B", "C"]] = df.loc[:, ["B", "C"]].astype(np.int64)
569-
else:
570-
df.loc[:, ["B", "C"]] = df.loc[:, ["B", "C"]].astype(np.int64)
571-
expected = DataFrame(
572-
[["1", 2, 3, ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
573-
)
574-
expected["B"] = expected["B"].astype(object)
575-
expected["C"] = expected["C"].astype(object)
576-
tm.assert_frame_equal(df, expected)
560+
df.loc[:, ["B", "C"]] = df.loc[:, ["B", "C"]].astype(np.int64)
561+
expected = DataFrame(
562+
[["1", 2, 3, ".4", 5, 6.0, "foo"]], columns=list("ABCDEFG")
563+
)
564+
expected[list("ABCDG")] = expected[list("ABCDG")].astype(object)
565+
tm.assert_frame_equal(df, expected)
577566

578567
def test_astype_assignment_full_replacements(self):
579568
# full replacements / no nans

0 commit comments

Comments
 (0)