Warning은 error는 아니라 해결은 안 해도 당장의 큰 문제는 없었지만, 계속 보이면... 짜증이 나기 마련이다.. ㅎ
이번에 value assignment 와 관련한 warning을 해결해 보았다.
C:\Users\: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['test'][i] = '0'
내가 원하는 것은 df의 test라는 column의 i 번째 row에 0을 지정하는 것이었는데, 접근자 e.g. at, loc 같은 것을 안 써줘서 "chained" assignments라고 혼동하게 된 것이다.
자세한 설명은 아래 링크에 가면 예시와 함께 나와있더라.
https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas
How to deal with SettingWithCopyWarning in Pandas
Background I just upgraded my Pandas from 0.11 to 0.13.0rc1. Now, the application is popping out many new warnings. One of them like this: E:\FinReporter\FM_EXT.py:449: SettingWithCopyWarning: A va...
stackoverflow.com
해결방법은 아래와 같다. 나의 경우 at을 사용했다.
df.at[i, 'test'] = '0'
좀 더 자세히 이해해 보자면, https://realpython.com/pandas-settingwithcopywarning/
SettingWithCopyWarning in pandas: Views vs Copies – Real Python
In this tutorial, you'll learn about views and copies in NumPy and pandas. You'll see why the SettingWithCopyWarning occurs in pandas and how to properly write code that avoids it.
realpython.com
- df[mask] 는 완전히 새로운 데이터 프레임 (보라색) 을 리턴한다. 우리는 의도하지 않았지만 이 복사본을 돌려받음.
- df[mask]["z"] = 0 는 이 새로운 복사된 데이터프레임(보라색)의 z열 값을 변경하지, 원본 df의 z 값을 변경하지는 않는다. 따라서 경고함!
배움을 기록하기 위한 공간입니다.
수정이 필요한 내용이나 공유하고 싶은 것이 있다면 언제든 댓글로 남겨주시면 환영입니다 :D
'배움 기록 > Programming' 카테고리의 다른 글
[python] argparse 사용법, verbose 란 ? (0) | 2023.04.03 |
---|---|
[python, pathlib, glob] 특정 directory (경로) 추출하기 (0) | 2023.03.29 |
[python] plt.savefig 이미지 잘리는 경우, 해결방법 3가지 (0) | 2023.03.27 |
[repository setup] packaging 관련 파일 - setup.py, setup.cfg 란? (0) | 2023.03.16 |
[JavaScript] Ajax를 이용해 FormData를 flask로 보내는 방법 (0) | 2023.01.06 |