inplace 오류메시지요
C:\Users\Public\Documents\ESTsoft\CreatorTemp\ipykernel_40236\2214525927.py:3: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment using an inplace method. Such inplace method never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write). For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' instead, to perform the operation inplace on the original object, or try to avoid an inplace operation using 'df[col] = df[col].method(value)'. See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html abandon_df['time_on_site'].fillna(abandon_df['time_on_site'].median(), inplace = True)
inplace를 쓰면 이 오류가 계속 뜹니다.
어떻게 해결해야 하나요? 무시해도 되는건가요? gpt는
abandon_df['time_on_site'] =abandon_df['time_on_site'].fillna(abandon_df['time_on_site'].median()
이방식이 요즘에는 맞다고 하는데…
뭐가 맞는걸까요
댓글
안녕하세요 질의주셔서 감사합니다.
해당 답변을 드리자면 무시하시면 안됩니다.
이번에 파이썬이 2.x 버전으로 업데이트가 되면서 inpace 파라미터를 비사용 권장으로 처리가 되었습니다.
Pandas 2.x부터 Copy-on-Write 정책이 적용되면서
df[col].method(..., inplace=True) 같은 체인 인덱싱 방식은
원본 DataFrame을 보장하지 않기 때문에 경고가 발생합니다.
그래서 현재 권장되는 방식은
abandon_df['time_on_site'] = abandon_df['time_on_site'].fillna( abandon_df['time_on_site'].median() )
처럼 명시적으로 다시 할당하는 방법입니다.
이 방식이 가장 안전하고, 앞으로도 유지될 공식적인 사용법으로 굳혀질 것 같습니다.
댓글을 남기려면 로그인하세요.