Failures & Issues/problem-solution

(python) what are NaN, Null, NA, None ?

metamong 2022. 4. 4.

-- python에 한정해서 (R 아님! 🤗) --

 

* None vs NaN(NA)

 

as of None...

 

☆ None is used to define a null value. It is not the same as an empty string, False, or a zero. It is a data type of the class NoneType object. Assigning a value of None to a variable is one way to reset it to its original, empty state.

 

→ 즉 None은 말그대로 missing data - 아무것도 없는, 데이터가 빠진 공간이라 생각하면 된다. 

→ 함수가 아무것도 return하지 않으면 None을 return

 NoneType object라는 특별한 type에 종속된다!

 

type(None) #NoneType

as of NaN... - numpy.nan

 

☆ None과 다르게 numerical operations 진행이 가능..! (NaN은 object type에 귀속되어 연산 내용에 포함되면 오류가 발생) numerically invalid하다 보면 됨(None은 empty인 반면)

 

 NA type을 일률적으로 NaN으로 변환하여 표현

→ type이 float이므로 operation 진행이 가능한 것 (including vectorized operations)

 

import numpy as np
type(np.nan) #float

 

<<공식 explanation>>

 

"Additionally, it exacts a fairly high performance cost when working with numerical data compared with the simple approach of using NaN. Thus, I have chosen the Pythonic “practicality beats purity” approach and traded integer NA capability for a much simpler approach of using a special value in float and object arrays to denote NA, and promoting integer arrays to floating when NAs must be introduced."

 

☆ python에서 null은 존재하지 않음. null의 의미로 None과 NaN으로 표현

 

 isnull() & fillna() method 모두 None과 NaN에 적용 가능하다

 

df.isnull()
df.fillna()

** handling NaN - docu) https://pandas.pydata.org/pandas-docs/dev/user_guide/gotchas.html

** source) https://www.geeksforgeeks.org/python-none-keyword/

댓글