diff --git a/asv_bench/benchmarks/join_merge.py b/asv_bench/benchmarks/join_merge.py index f6630857bee91..0313f956f8e04 100644 --- a/asv_bench/benchmarks/join_merge.py +++ b/asv_bench/benchmarks/join_merge.py @@ -359,11 +359,11 @@ def setup(self, direction, tolerance): df1 = df1.sort_values("time") df2 = df2.sort_values("time") - df1["time32"] = np.int32(df1.time) - df2["time32"] = np.int32(df2.time) + df1.loc[:, "time32"] = np.int32(df1.time) + df2.loc[:, "time32"] = np.int32(df2.time) - df1["timeu64"] = np.uint64(df1.time) - df2["timeu64"] = np.uint64(df2.time) + df1.loc[:, "timeu64"] = np.uint64(df1.time) + df2.loc[:, "timeu64"] = np.uint64(df2.time) self.df1a = df1[["time", "value1"]] self.df2a = df2[["time", "value2"]] diff --git a/pandas/tests/arrays/categorical/test_indexing.py b/pandas/tests/arrays/categorical/test_indexing.py index 94e966642b925..bbae60dbcc10d 100644 --- a/pandas/tests/arrays/categorical/test_indexing.py +++ b/pandas/tests/arrays/categorical/test_indexing.py @@ -112,7 +112,7 @@ def test_setitem_listlike(self): np.random.randint(0, 5, size=150000).astype(np.int8) ).add_categories([-1000]) indexer = np.array([100000]).astype(np.int64) - cat[indexer] = -1000 + cat.loc[indexer] = -1000 # we are asserting the code result here # which maps to the -1000 category diff --git a/pandas/tests/arrays/categorical/test_operators.py b/pandas/tests/arrays/categorical/test_operators.py index 9642691b5c578..62b2553dc97aa 100644 --- a/pandas/tests/arrays/categorical/test_operators.py +++ b/pandas/tests/arrays/categorical/test_operators.py @@ -351,7 +351,7 @@ def test_numeric_like_ops(self): cat_labels = Categorical(labels, labels) df = df.sort_values(by=["value"], ascending=True) - df["value_group"] = pd.cut( + df.loc[:, "value_group"] = pd.cut( df.value, range(0, 10500, 500), right=False, labels=cat_labels ) diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index c30ffcf9a7e5b..c0402918b5495 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -843,7 +843,7 @@ def test_setitem_with_expansion_categorical_dtype(self): cat = ser.values # setting with a Categorical - df["D"] = cat + df.loc[:, "D"] = cat str(df) result = df.dtypes @@ -854,7 +854,7 @@ def test_setitem_with_expansion_categorical_dtype(self): tm.assert_series_equal(result, expected) # setting with a Series - df["E"] = ser + df.loc[: "E"] = ser str(df) result = df.dtypes diff --git a/pandas/tests/frame/methods/test_describe.py b/pandas/tests/frame/methods/test_describe.py index 24d327a101143..334c0b4862128 100644 --- a/pandas/tests/frame/methods/test_describe.py +++ b/pandas/tests/frame/methods/test_describe.py @@ -97,7 +97,7 @@ def test_describe_categorical(self): cat_labels = Categorical(labels, labels) df = df.sort_values(by=["value"], ascending=True) - df["value_group"] = pd.cut( + df.loc[:,"value_group"] = pd.cut( df.value, range(0, 10500, 500), right=False, labels=cat_labels ) cat = df diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 69e5d5e3d5447..df277d3b8b0a2 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -584,7 +584,7 @@ def test_unstack_dtypes(self): # mixed df2 = df.set_index(["A", "B"]) - df2["C"] = 3.0 + df2.loc[:, "C"] = 3.0 df3 = df2.unstack("B") result = df3.dtypes expected = Series( @@ -594,7 +594,7 @@ def test_unstack_dtypes(self): ), ) tm.assert_series_equal(result, expected) - df2["D"] = "foo" + df2.loc[:, "D"] = "foo" df3 = df2.unstack("B") result = df3.dtypes expected = Series( @@ -626,7 +626,7 @@ def test_unstack_dtypes_mixed_date(self, c, d): right = df.iloc[:3].copy(deep=True) df = df.set_index(["A", "B"]) - df["D"] = df["D"].astype("int64") + df.loc[:, "D"] = df["D"].astype("int64") left = df.iloc[:3].unstack(0) right = right.set_index(["A", "B"]).unstack(0) @@ -2007,7 +2007,7 @@ def test_unstack_with_missing_int_cast_to_float(self, using_array_manager): ).set_index(["a", "b"]) # add another int column to get 2 blocks - df["is_"] = 1 + df.loc[:, "is_"] = 1 if not using_array_manager: assert len(df._mgr.blocks) == 2 diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index e99d1325a7e4f..9dda873c82209 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -914,7 +914,7 @@ def test_sort(): cat_labels = Categorical(labels, labels) df = df.sort_values(by=["value"], ascending=True) - df["value_group"] = pd.cut( + df.loc[:, "value_group"] = pd.cut( df.value, range(0, 10500, 500), right=False, labels=cat_labels ) diff --git a/pandas/tests/groupby/test_nth.py b/pandas/tests/groupby/test_nth.py index 187c80075f36b..32489b0e32b91 100644 --- a/pandas/tests/groupby/test_nth.py +++ b/pandas/tests/groupby/test_nth.py @@ -212,7 +212,7 @@ def test_nth(): tm.assert_frame_equal(g.nth(0, dropna="any"), exp.iloc[[1, 2]]) tm.assert_frame_equal(g.nth(-1, dropna="any"), exp.iloc[[1, 2]]) - exp["B"] = np.nan + exp.loc[:, "B"] = np.nan tm.assert_frame_equal(g.nth(7, dropna="any"), exp.iloc[[1, 2]]) tm.assert_frame_equal(g.nth(2, dropna="any"), exp.iloc[[1, 2]]) diff --git a/pandas/tests/groupby/test_value_counts.py b/pandas/tests/groupby/test_value_counts.py index 577a72d3f5090..359a248941d18 100644 --- a/pandas/tests/groupby/test_value_counts.py +++ b/pandas/tests/groupby/test_value_counts.py @@ -131,7 +131,7 @@ def test_series_groupby_value_counts_with_grouper(): } ).drop([3]) - df["Datetime"] = to_datetime(df["Timestamp"].apply(lambda t: str(t)), unit="s") + df.loc[:, "Datetime"] = to_datetime(df["Timestamp"].apply(lambda t: str(t)), unit="s") dfg = df.groupby(Grouper(freq="1D", key="Datetime")) # have to sort on index because of unstable sort on values xref GH9212 diff --git a/pandas/tests/indexing/multiindex/test_getitem.py b/pandas/tests/indexing/multiindex/test_getitem.py index 3790a6e9a5319..e3251ebf8da6e 100644 --- a/pandas/tests/indexing/multiindex/test_getitem.py +++ b/pandas/tests/indexing/multiindex/test_getitem.py @@ -209,25 +209,25 @@ def test_frame_getitem_nan_multiindex(nulls_fixture): [[11, n, 13], [21, n, 23], [31, n, 33], [41, n, 43]], columns=cols, ).set_index(["a", "b"]) - df["c"] = df["c"].astype("int64") + df.loc[:, "c"] = df["c"].astype("int64") idx = (21, n) result = df.loc[:idx] expected = DataFrame([[11, n, 13], [21, n, 23]], columns=cols).set_index(["a", "b"]) - expected["c"] = expected["c"].astype("int64") + expected.loc[:, "c"] = expected["c"].astype("int64") tm.assert_frame_equal(result, expected) result = df.loc[idx:] expected = DataFrame( [[21, n, 23], [31, n, 33], [41, n, 43]], columns=cols ).set_index(["a", "b"]) - expected["c"] = expected["c"].astype("int64") + expected.loc[:, "c"] = expected["c"].astype("int64") tm.assert_frame_equal(result, expected) idx1, idx2 = (21, n), (31, n) result = df.loc[idx1:idx2] expected = DataFrame([[21, n, 23], [31, n, 33]], columns=cols).set_index(["a", "b"]) - expected["c"] = expected["c"].astype("int64") + expected.loc[:, "c"] = expected["c"].astype("int64") tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/indexing/multiindex/test_iloc.py b/pandas/tests/indexing/multiindex/test_iloc.py index db91d5ad88252..86b988b73c5a2 100644 --- a/pandas/tests/indexing/multiindex/test_iloc.py +++ b/pandas/tests/indexing/multiindex/test_iloc.py @@ -159,7 +159,7 @@ def test_iloc_setitem_int_multiindex_series(data, indexes, values, expected_k): for i, v in zip(indexes, values): series.iloc[i] += v - df["k"] = expected_k + df.loc[:, "k"] = expected_k expected = df.k tm.assert_series_equal(series, expected) diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 81914e1b8052f..5d58dc0f19041 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -379,7 +379,7 @@ def test_detect_chained_assignment_is_copy(self): # an identical take, so no copy df = DataFrame({"a": [1]}).dropna() assert df._is_copy is None - df["a"] += 1 + df.loc[:, "a"] += 1 @pytest.mark.arm_slow def test_detect_chained_assignment_sorting(self): diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index 231228ef6c0af..afc9aad4cf434 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -391,7 +391,7 @@ def test_record_prefix(self, state_data): expected.extend(rec["counties"]) expected = DataFrame(expected) expected = expected.rename(columns=lambda x: "county_" + x) - expected["state"] = np.array(["Florida", "Ohio"]).repeat([3, 2]) + expected.loc[:, "state"] = np.array(["Florida", "Ohio"]).repeat([3, 2]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/io/pytables/test_append.py b/pandas/tests/io/pytables/test_append.py index cae6c4924015d..08dca4b6e815d 100644 --- a/pandas/tests/io/pytables/test_append.py +++ b/pandas/tests/io/pytables/test_append.py @@ -840,7 +840,7 @@ def test_append_with_timedelta(setup_path): def test_append_to_multiple(setup_path): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns="{}_2".format) - df2["foo"] = "bar" + df2.loc[:, "foo"] = "bar" df = concat([df1, df2], axis=1) with ensure_clean_store(setup_path) as store: diff --git a/pandas/tests/io/pytables/test_select.py b/pandas/tests/io/pytables/test_select.py index e26b47d6615bd..08bba54314cf6 100644 --- a/pandas/tests/io/pytables/test_select.py +++ b/pandas/tests/io/pytables/test_select.py @@ -375,7 +375,7 @@ def test_select_iterator(setup_path): df1 = tm.makeTimeDataFrame(500) store.append("df1", df1, data_columns=True) df2 = tm.makeTimeDataFrame(500).rename(columns="{}_2".format) - df2["foo"] = "bar" + df2.loc[:, "foo"] = "bar" store.append("df2", df2) df = concat([df1, df2], axis=1) @@ -790,7 +790,7 @@ def test_select_as_multiple(setup_path): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns="{}_2".format) - df2["foo"] = "bar" + df2.loc[:, "foo"] = "bar" with ensure_clean_store(setup_path) as store: diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 8888e2687621d..c6e005e284d15 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -845,7 +845,7 @@ def test_idxmin(self): string_series = tm.makeStringSeries().rename("series") # add some NaNs - string_series[5:15] = np.NaN + string_series.iloc[5:15] = np.NaN # skipna or no assert string_series[string_series.idxmin()] == string_series.min() @@ -875,7 +875,7 @@ def test_idxmax(self): string_series = tm.makeStringSeries().rename("series") # add some NaNs - string_series[5:15] = np.NaN + string_series.iloc[5:15] = np.NaN # skipna or no assert string_series[string_series.idxmax()] == string_series.max() diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 7fe1e645aa141..5c1036fd78a0d 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -511,7 +511,7 @@ def test_resample_empty_Dataframe(keys): df = df.set_index("date") result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) - expected["date"] = pd.to_datetime(expected["date"]) + expected.loc[:, "date"] = pd.to_datetime(expected["date"]) expected = expected.set_index("date", append=True, drop=True) if len(keys) == 1: expected.index.name = keys[0] diff --git a/pandas/tests/reshape/test_get_dummies.py b/pandas/tests/reshape/test_get_dummies.py index 6c9a60caaa2be..c829a983aa480 100644 --- a/pandas/tests/reshape/test_get_dummies.py +++ b/pandas/tests/reshape/test_get_dummies.py @@ -548,7 +548,7 @@ def test_dataframe_dummies_drop_first_with_na(self, df, sparse): expected = expected.sort_index(axis=1) if sparse: for col in cols: - expected[col] = SparseArray(expected[col]) + expected.loc[:, col] = SparseArray(expected[col]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 38727aa20ff4c..5a3ff42c27c9f 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -993,7 +993,7 @@ def test_margins_dtype(self): expected = DataFrame( {"dull": [12, 21, 3, 9, 45], "shiny": [33, 0, 36, 51, 120]}, index=mi ).rename_axis("C", axis=1) - expected["All"] = expected["dull"] + expected["shiny"] + expected.loc[:, "All"] = expected["dull"] + expected["shiny"] result = df.pivot_table( values="D", @@ -1012,7 +1012,7 @@ def test_margins_dtype_len(self): expected = DataFrame( {"dull": [1, 1, 2, 1, 5], "shiny": [2, 0, 2, 2, 6]}, index=mi ).rename_axis("C", axis=1) - expected["All"] = expected["dull"] + expected["shiny"] + expected.loc[:, "All"] = expected["dull"] + expected["shiny"] result = self.data.pivot_table( values="D", diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index e3df9671e6c64..e2bb46952742e 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -41,7 +41,7 @@ def test_basic_indexing(): s[5] msg = r"index 5 is out of bounds for axis (0|1) with size 5|^5$" with pytest.raises(IndexError, match=msg): - s[5] = 0 + s.loc[5] = 0 def test_basic_getitem_with_labels(datetime_series): diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index ac12b513aad4e..9399256bcb0c2 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -312,7 +312,7 @@ def test_timedelta_fillna(self, frame_or_series): obj = frame_or_series(td) result = obj.ffill() expected = td.fillna(Timedelta(seconds=0)) - expected[0] = np.nan + expected.loc[:, 0] = np.nan expected = frame_or_series(expected) tm.assert_equal(result, expected) @@ -322,7 +322,7 @@ def test_timedelta_fillna(self, frame_or_series): obj = frame_or_series(td) result = obj.bfill() expected = td.fillna(Timedelta(seconds=0)) - expected[2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1) + expected.loc[:, 2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1) expected = frame_or_series(expected) tm.assert_equal(result, expected) diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index 38ac6bb2e1c09..226aaf93fd7f0 100644 --- a/pandas/tests/window/test_groupby.py +++ b/pandas/tests/window/test_groupby.py @@ -585,7 +585,7 @@ def test_groupby_rolling_string_index(self): ).set_index("index") groups = df.groupby("group") - df["count_to_date"] = groups.cumcount() + df.loc[:, "count_to_date"] = groups.cumcount() rolling_groups = groups.rolling("10d", on="eventTime") result = rolling_groups.apply(lambda df: df.shape[0]) expected = DataFrame( diff --git a/pyproject.toml b/pyproject.toml index 1452d301703dd..77a4b4449a93e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ exclude = ''' [tool.pylint.messages_control] max-line-length = 88 +#"unsupported-assignment-operation", disable = [ "C", "R", @@ -57,7 +58,7 @@ disable = [ "unexpected-keyword-arg", "unpacking-non-sequence", "unsubscriptable-object", - "unsupported-assignment-operation", + "unsupported-membership-test", "used-before-assignment", ]