Data/Python

[Python]NaN, None 차이(feat. Pandas.NA)

뚱요 2021. 6. 27. 00:00
반응형

Python에서는 결측값을 표현하는데에 NaN과 None을 사용하는데 둘의 차이에 대해서 알아보겠습니다. 정의, 데이터 타입, 객체 생성 측면에서 차이점을 보고 pd.NA가 왜 있는지에 대해서 알아보겠습니다.

참고로 파이썬에서는 null, Na없는데 몇몇 블로그에서는 alias로 불러오거나 객체 만들어서 있다고 설명하셨다. 또, 동일한 의미라고 설명되어 있는데 맞지 않는 설명이라고 생각합니다. 동일하다면 굳이 따로 만들 필요도 없고 다르게 정의할 필요도 없다고 생각합니다.

1. 정의

NaN(Not A Number)

표현되지 않는 부동소수점 값으로  float 타입이다. 사용하는 방법은 하단과 같이 두가지 방식이다. nan 스트링에다가 float으로 강제 형변환하거나 넘파이의 nan을 불러온다.

float('nan')

import numpy as np
np.nan

 IEEE 754 부동소수점 표준 참고: https://en.wikipedia.org/wiki/IEEE_floating_point

 

None 

Python에서의 존재하지 않음(NULL)을 의미하며 비어있는 리스트, 딕셔너리, 세트, 스트링, 0,  False 가 해당된다.

 

2. 데이터 타입

 NaN은 float 타입 , None  NoneType 이다. NaN 은  NaN이 결과로 나오긴 하지만 수치 계산에 사용 가능하다.

 

  NaN None
type float None
계산 가능 불가

타입이 다르기 때문에 계산이 불가하다는 타입에러가 나온다.

 

3. 객체 생성

  • is 연산자는 해당 객체의 주소(포인터) 비교
  • == 연산자  값 비교

하나만 생성하는 싱글톤인 None 은 동일한 id를 갖기 때문에 언제나 True가 된다. 그러나 NaN의 경우 각각의 float 객체를 생성한 것이므로 is를 사용하면 False가 된다.

 

4. Pandas에서의 NaN, None과 pd.NA

히지만 판다스에서는 결측치를 찾거나 결측치 제거, 결측치 대체 할때 NaN, None을 동일하게 처리 함을 알 수 있다. None이 있으면 자동으로 NaN으로 변경해준다. 이는 Pandas에서 처리속도와 편의성때문에 이렇게 채택했다. 1차원, 2차원 행렬에서 동일한 타입을 같이 처리하는 판다스에서는 None 값은 결측치이기 때문이다.

판다스의 notnull(), isnull(), fillna(), dropna() 모두 NaN, None에게 동일하게 결측치(Missing value)로 보고 적용 된다. 판다스 공식문서를 보면 NaN, None, NaT을 동일하게 처리한다 (NaN in numeric arrays, None or NaN in object arrays, NaT in datetimelike) 

 

Pandas.NA

그렇다면 NaN이 있는데 왜 판다스에서는 따로 pd.NA로 따로 결측치를 제공하고 있을까? 이전에는 experimental NA라고 불렸고 제공하는 이유는 np.NaN이나 None을 사용하게 되면 자동으로 타입캐스팅 되어 수치형은 float이 된다. integer타입의 강제 형변환 방지하기 위해서 제공한다. 리턴도 <NA>로 나오는 것을 알 수 있다.

 

 

 

출처:

https://pandas.pydata.org/pandas-docs/version/1.1/user_guide/missing_data.html

 

Working with missing data — pandas 1.1.5 documentation

Starting from pandas 1.0, an experimental pd.NA value (singleton) is available to represent scalar missing values. At this moment, it is used in the nullable integer, boolean and dedicated string data types as the missing value indicator. The goal of pd.NA

pandas.pydata.org

https://pandas.pydata.org/docs/reference/api/pandas.isnull.html

 

pandas.isnull — pandas 1.4.3 documentation

For scalar input, returns a scalar boolean. For array input, returns an array of boolean indicating whether each corresponding element is missing.

pandas.pydata.org

https://realpython.com/null-in-python/

 

Null in Python: Understanding Python's NoneType Object – Real Python

In this tutorial, you'll learn about the NoneType object None, which acts as the null in Python. This object represents emptiness, and you can use it to mark default parameters and even show when you have no result. None is a tool for doing everything with

realpython.com

https://pandas.pydata.org/docs/user_guide/integer_na.html#integer-na

 

Nullable integer data type — pandas 1.4.3 documentation

Nullable integer data type Note IntegerArray is currently experimental. Its API or implementation may change without warning. Changed in version 1.0.0: Now uses pandas.NA as the missing value rather than numpy.nan. In Working with missing data, we saw that

pandas.pydata.org

 

반응형