Skip to content

Commit 145a414

Browse files
authored
REF: misplaced DataFrame.where tests (#32948)
1 parent e444e61 commit 145a414

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

pandas/tests/frame/indexing/test_where.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,40 @@ def test_where_tz_values(self, tz_naive_fixture):
591591
)
592592
result = df1.where(mask, df2)
593593
tm.assert_frame_equal(exp, result)
594+
595+
def test_df_where_change_dtype(self):
596+
# GH#16979
597+
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))
598+
mask = np.array([[True, False, False], [False, False, True]])
599+
600+
result = df.where(mask)
601+
expected = DataFrame(
602+
[[0, np.nan, np.nan], [np.nan, np.nan, 5]], columns=list("ABC")
603+
)
604+
605+
tm.assert_frame_equal(result, expected)
606+
607+
@pytest.mark.parametrize("kwargs", [dict(), dict(other=None)])
608+
def test_df_where_with_category(self, kwargs):
609+
# GH#16979
610+
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))
611+
mask = np.array([[True, False, False], [False, False, True]])
612+
613+
# change type to category
614+
df.A = df.A.astype("category")
615+
df.B = df.B.astype("category")
616+
df.C = df.C.astype("category")
617+
618+
result = df.where(mask, **kwargs)
619+
A = pd.Categorical([0, np.nan], categories=[0, 3])
620+
B = pd.Categorical([np.nan, np.nan], categories=[1, 4])
621+
C = pd.Categorical([np.nan, 5], categories=[2, 5])
622+
expected = DataFrame({"A": A, "B": B, "C": C})
623+
624+
tm.assert_frame_equal(result, expected)
625+
626+
# Check Series.where while we're here
627+
result = df.A.where(mask[:, 0], **kwargs)
628+
expected = Series(A, name="A")
629+
630+
tm.assert_series_equal(result, expected)

pandas/tests/frame/test_dtypes.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,6 @@ def test_astype_extension_dtypes_duplicate_col(self, dtype):
453453
expected = concat([a1.astype(dtype), a2.astype(dtype)], axis=1)
454454
tm.assert_frame_equal(result, expected)
455455

456-
@pytest.mark.parametrize("kwargs", [dict(), dict(other=None)])
457-
def test_df_where_with_category(self, kwargs):
458-
# GH 16979
459-
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))
460-
mask = np.array([[True, False, True], [False, True, True]])
461-
462-
# change type to category
463-
df.A = df.A.astype("category")
464-
df.B = df.B.astype("category")
465-
df.C = df.C.astype("category")
466-
467-
result = df.A.where(mask[:, 0], **kwargs)
468-
expected = Series(pd.Categorical([0, np.nan], categories=[0, 3]), name="A")
469-
470-
tm.assert_series_equal(result, expected)
471-
472456
@pytest.mark.parametrize(
473457
"dtype", [{100: "float64", 200: "uint64"}, "category", "float64"]
474458
)
@@ -479,31 +463,6 @@ def test_astype_column_metadata(self, dtype):
479463
df = df.astype(dtype)
480464
tm.assert_index_equal(df.columns, columns)
481465

482-
def test_df_where_change_dtype(self):
483-
# GH 16979
484-
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))
485-
mask = np.array([[True, False, False], [False, False, True]])
486-
487-
result = df.where(mask)
488-
expected = DataFrame(
489-
[[0, np.nan, np.nan], [np.nan, np.nan, 5]], columns=list("ABC")
490-
)
491-
492-
tm.assert_frame_equal(result, expected)
493-
494-
# change type to category
495-
df.A = df.A.astype("category")
496-
df.B = df.B.astype("category")
497-
df.C = df.C.astype("category")
498-
499-
result = df.where(mask)
500-
A = pd.Categorical([0, np.nan], categories=[0, 3])
501-
B = pd.Categorical([np.nan, np.nan], categories=[1, 4])
502-
C = pd.Categorical([np.nan, 5], categories=[2, 5])
503-
expected = DataFrame({"A": A, "B": B, "C": C})
504-
505-
tm.assert_frame_equal(result, expected)
506-
507466
@pytest.mark.parametrize("dtype", ["M8", "m8"])
508467
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])
509468
def test_astype_from_datetimelike_to_object(self, dtype, unit):

0 commit comments

Comments
 (0)