Skip to content

TST Verifiy that dropna returns none when called inplace (#35179) #35181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def test_dropEmptyRows(self, float_frame):
smaller_frame = frame.dropna(how="all")
# check that original was preserved
tm.assert_series_equal(frame["foo"], original)
inplace_frame1.dropna(how="all", inplace=True)
return_value = inplace_frame1.dropna(how="all", inplace=True)
tm.assert_series_equal(smaller_frame["foo"], expected)
tm.assert_series_equal(inplace_frame1["foo"], expected)
assert return_value is None

smaller_frame = frame.dropna(how="all", subset=["foo"])
inplace_frame2.dropna(how="all", subset=["foo"], inplace=True)
return_value = inplace_frame2.dropna(how="all", subset=["foo"], inplace=True)
tm.assert_series_equal(smaller_frame["foo"], expected)
tm.assert_series_equal(inplace_frame2["foo"], expected)
assert return_value is None

def test_dropIncompleteRows(self, float_frame):
N = len(float_frame.index)
Expand All @@ -45,18 +47,20 @@ def test_dropIncompleteRows(self, float_frame):

smaller_frame = frame.dropna()
tm.assert_series_equal(frame["foo"], original)
inp_frame1.dropna(inplace=True)
return_value = inp_frame1.dropna(inplace=True)

exp = Series(mat[5:], index=float_frame.index[5:], name="foo")
tm.assert_series_equal(smaller_frame["foo"], exp)
tm.assert_series_equal(inp_frame1["foo"], exp)
assert return_value is None

samesize_frame = frame.dropna(subset=["bar"])
tm.assert_series_equal(frame["foo"], original)
assert (frame["bar"] == 5).all()
inp_frame2.dropna(subset=["bar"], inplace=True)
return_value = inp_frame2.dropna(subset=["bar"], inplace=True)
tm.assert_index_equal(samesize_frame.index, float_frame.index)
tm.assert_index_equal(inp_frame2.index, float_frame.index)
assert return_value is None

def test_dropna(self):
df = DataFrame(np.random.randn(6, 4))
Expand All @@ -65,31 +69,35 @@ def test_dropna(self):
dropped = df.dropna(axis=1)
expected = df.loc[:, [0, 1, 3]]
inp = df.copy()
inp.dropna(axis=1, inplace=True)
return_value = inp.dropna(axis=1, inplace=True)
tm.assert_frame_equal(dropped, expected)
tm.assert_frame_equal(inp, expected)
assert return_value is None

dropped = df.dropna(axis=0)
expected = df.loc[list(range(2, 6))]
inp = df.copy()
inp.dropna(axis=0, inplace=True)
return_value = inp.dropna(axis=0, inplace=True)
tm.assert_frame_equal(dropped, expected)
tm.assert_frame_equal(inp, expected)
assert return_value is None

# threshold
dropped = df.dropna(axis=1, thresh=5)
expected = df.loc[:, [0, 1, 3]]
inp = df.copy()
inp.dropna(axis=1, thresh=5, inplace=True)
return_value = inp.dropna(axis=1, thresh=5, inplace=True)
tm.assert_frame_equal(dropped, expected)
tm.assert_frame_equal(inp, expected)
assert return_value is None

dropped = df.dropna(axis=0, thresh=4)
expected = df.loc[range(2, 6)]
inp = df.copy()
inp.dropna(axis=0, thresh=4, inplace=True)
return_value = inp.dropna(axis=0, thresh=4, inplace=True)
tm.assert_frame_equal(dropped, expected)
tm.assert_frame_equal(inp, expected)
assert return_value is None

dropped = df.dropna(axis=1, thresh=4)
tm.assert_frame_equal(dropped, df)
Expand All @@ -100,9 +108,10 @@ def test_dropna(self):
# subset
dropped = df.dropna(axis=0, subset=[0, 1, 3])
inp = df.copy()
inp.dropna(axis=0, subset=[0, 1, 3], inplace=True)
return_value = inp.dropna(axis=0, subset=[0, 1, 3], inplace=True)
tm.assert_frame_equal(dropped, df)
tm.assert_frame_equal(inp, df)
assert return_value is None

# all
dropped = df.dropna(axis=1, how="all")
Expand All @@ -126,12 +135,14 @@ def test_drop_and_dropna_caching(self):
df2 = df.copy()
df["A"].dropna()
tm.assert_series_equal(df["A"], original)
df["A"].dropna(inplace=True)
return_value = df["A"].dropna(inplace=True)
tm.assert_series_equal(df["A"], expected)
assert return_value is None
df2["A"].drop([1])
tm.assert_series_equal(df2["A"], original)
df2["A"].drop([1], inplace=True)
return_value = df2["A"].drop([1], inplace=True)
tm.assert_series_equal(df2["A"], original.drop([1]))
assert return_value is None

def test_dropna_corner(self, float_frame):
# bad input
Expand Down Expand Up @@ -251,8 +262,9 @@ def test_fillna_different_dtype(self):
)
tm.assert_frame_equal(result, expected)

df.fillna({2: "foo"}, inplace=True)
return_value = df.fillna({2: "foo"}, inplace=True)
tm.assert_frame_equal(df, expected)
assert return_value is None

def test_fillna_limit_and_value(self):
# limit and value
Expand Down