본문 바로가기

Python

[Pandas] FutureWarning 에러 - Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas.

문제 상황

판다스를 활용한 함수를 활용하여 알고리즘 실행 시 결과값 생성에 이상은 없으나 아래와 같은 경고메세지를 출력하고 있는 상황이다.

FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.45399999999999996' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.

Pandas를 사용하여 데이터 프레임을 다루는 과정에서 특정 열에 서로 호환되지 않는 데이터 유형을 할당하려 할 때 FutureWarning 경고 메시지가 발생한다.

데이터 타입의 불일치를 나타내며, 향후 버전에서 오류로 발전할 가능성이 있다.

원인

new_df["floor"] 열이 int64 형식으로 설정되어 있었으나, 소수점 값을 가진 rate_를 할당하려고 하면서 데이터 타입 불일치가 발생했다. Pandas는 서로 다른 데이터 타입을 허용하지 않기 때문에 경고 메시지가 출력되는 것

해결 과정

new_df["floor"] = 0.0  # 초기값을 float으로 설정

for i in df.index:
    # ...

    rate = rate / 100  # 100%를 1로 전환
    new_df.loc[i, "floor"] = float(rate)  # 명시적으로 float으로 변환
  1. new_df["floor"] 열의 초기값을 0.0으로 설정하여 float 형식으로 변경
  2. 소수점 값을 할당할 때 명시적으로 float으로 변환하여 데이터 타입 일관성 유지