import warnings import pandas as pd def frame(): return pd.DataFrame( { "name": ["Ada", "Lin", "Bo"], "score": [95, 88, 91], } ) def score_for(df, name): row = df["name"].eq(name) return int(df.loc[row, "score"].iloc[0]) print(f"pandas {pd.__version__}") df = frame() with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") df["score"][df["name"].eq("Lin")] = 100 warning_name = caught[0].category.__name__ if caught else "none" print(f"chained assignment warning: {warning_name}") print(f"Lin after chained assignment: {score_for(df, 'Lin')}") df = frame() df.loc[df["name"].eq("Lin"), "score"] = 100 print(f"Lin after loc rewrite: {score_for(df, 'Lin')}") df = frame() with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") df["score"].replace(88, 100, inplace=True) warning_name = caught[0].category.__name__ if caught else "none" print(f"inplace column warning: {warning_name}") print(f"Lin after inplace replace: {score_for(df, 'Lin')}") df["score"] = df["score"].replace(88, 100) print(f"Lin after column reassignment: {score_for(df, 'Lin')}") df = frame() score_view = df["score"] score_view.iloc[0] = 10 print(f"Ada parent score after Series mutation: {score_for(df, 'Ada')}") df = frame() arr = df["score"].to_numpy() try: arr[0] = 10 except ValueError as exc: print(f"NumPy view write: {exc}") arr = df["score"].to_numpy().copy() arr[0] = 10 print(f"copied NumPy array first value: {int(arr[0])}") print(f"Ada parent score after array copy: {score_for(df, 'Ada')}")