๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/AI

(AI) FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.The behavior will change in pandas 3.0

by gomdeng 2024. 12. 27.

๐Ÿถ ์—๋Ÿฌ

๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ์—์„œ, ํŠน์ • ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ๊ฒฐ์ธก์น˜ ๋Œ€์ฒด๊ฐ’ ์ฒ˜๋ฆฌ ๊ณผ์ • ์†์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

โœ”๏ธ ์ž…๋ ฅ
df['CRIM'].fillna(df['CRIM'].median(), inplace=True)

โœ”๏ธ ์—๋Ÿฌ
FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy

 

๐Ÿถ ๋‚ด์šฉ

1. Pandas์˜ FutureWarning ์œผ๋กœ, DataFrame์„ ์ˆ˜์ •ํ•˜๋Š” ์ฝ”๋“œ์—์„œ  "์ฒด์ด๋‹(chaining)" ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ๋•Œ ๋ฐœ์ƒ
โœ”๏ธ ์ฒด์ด๋‹(chaining) ๋ฌธ์ œ 
 - Pandas ์ฝ”๋“œ์—์„œ ๋ฉ”์„œ๋“œ ์ฒด์ธ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์„ ํƒํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ
 - ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ๋ทฐ(view)์™€ ๋ณต์‚ฌ๋ณธ(copy) ์‚ฌ์ด์˜ ๋ถˆ๋ช…ํ™•ํ•œ ๋™์ž‘ ๋•Œ๋ฌธ์— ๋ฐœ์ƒ
 - ๋ฐ์ดํ„ฐ์˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ˆ˜์ •, ์„ฑ๋Šฅ ์ €ํ•˜ ๋ฐœ์ƒ
 - SettingWithCopyWarning ๋˜๋Š” FutureWarning ๊ฐ™์€ ๊ฒฝ๊ณ ๋ฅผ ์œ ๋ฐœ

2. inplace=True๋Š” Pandas 3.0์—์„œ ๋” ์ด์ƒ ์ง€์›๋˜์ง€ ์•Š์„ ์˜ˆ์ •
โœ”๏ธ ์ด์œ 
 - ์ฝ”๋“œ์˜ ๋ช…ํ™•์„ฑ, ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ์™€ ๋ทฐ์˜ ๋ถˆ๋ช…ํ™•์„ฑ, ์ผ๊ด€์„ฑ ๋ฌธ์ œ ํ•ด๊ฒฐ

 - ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ์™€ ๋ทฐ(View) ์‚ฌ์ด์˜ ํ˜ผ๋ž€
  >  Pandas์—์„œ inplace=True๋Š” DataFrame์ด๋‚˜ Series๋ฅผ "์›๋ณธ"์—์„œ ์ˆ˜์ •ํ•œ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์ง€๋งŒ
     ์‹ค์ œ๋กœ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Œ.
  > Pandas๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ณต์‚ฌ๋ฅผ ํ•  ๋•Œ์™€ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๊ฐ€ ์žˆ์–ด,
     ์‚ฌ์šฉ์ž๊ฐ€ ์˜๋„ํ•˜์ง€ ์•Š์€ ๋™์ž‘์„ ์œ ๋ฐœํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ
 - Pandas API์˜ ์ผ๊ด€์„ฑ ์œ ์ง€
  > Pandas์˜ ๋งŽ์€ ํ•จ์ˆ˜๋“ค์€ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™
  > inplace=True ๋Š” ์ผ๋ถ€ ํ•จ์ˆ˜์—์„œ๋งŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๊ด€์„ฑ์ด ๋–จ์–ด์ง
 - ์—๋Ÿฌ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ๋””๋ฒ„๊น…ํ•˜๊ธฐ ์–ด๋ ค์›€
  > ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •ํ™•ํžˆ ์–ด๋–ป๊ฒŒ ์ˆ˜์ •๋˜์—ˆ๋Š”์ง€ ์ถ”์ ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง
  > ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ

 

๐Ÿถ ์กฐ์น˜

1. ๋ช…์‹œ์ ์œผ๋กœ ์—ด์„ ์ˆ˜์ •ํ•˜๊ธฐ
df['CRIM'] = df['CRIM'].fillna(df['CRIM'].median())

2. loc ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ์ˆ˜์ •
df.loc[:, 'CRIM'] = df['CRIM'].fillna(df['CRIM'].median())