Skip to content

add a test for loc method; check if a warning raise when replacing a … #36486

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 11 commits into from
Sep 22, 2020
15 changes: 15 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pandas import DataFrame, Series, Timestamp, date_range
import pandas._testing as tm
from pandas.api.types import is_scalar
import pandas.core.common as com
from pandas.tests.indexing.common import Base


Expand Down Expand Up @@ -1130,3 +1131,17 @@ def test_loc_with_period_index_indexer():
tm.assert_frame_equal(df, df.loc[list(idx)])
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
tm.assert_frame_equal(df, df.loc[list(idx)])


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put in pandas/tests/indexing/test_chaining_and_caching.py instead with the rest of these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Contributor Author

@samilAyoub samilAyoub Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a function called test_detect_chained_assignment_warnings that test if chained assignment raise a warning SettingWithCopyWarning in the case of warn option. We need just modify that function to detect also SettingWithCopyError in the case of raise option.

def test_loc_replace_subset_with_subset():
# GH#36424 Should raise a SettingWithCopyError
df1 = pd.DataFrame(
data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=["A", "B", "C"],
index=[0, 0, 1],
)
df2 = df1.copy()
df2[:] = np.nan
msg = "A value is trying to be set on a copy of a slice from a DataFrame."
with pytest.raises(com.SettingWithCopyError, match=msg):
df2.loc[0]["A"] = df1.loc[0]["A"]