Python None vs Numpy Nan

chenshuai_2004 2017-02-27 11:57:16
python原生的None和pandas, numpy中的numpy.NaN在功能上都是用来标示空缺数据

a)None是一个python特殊的数据类型, 但是NaN却是用一个特殊的float
print( type(None) ,type(np.nan)) --><class 'NoneType'> <class 'float'>

sum([1,2,None]) -->TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
sum([1,2,np.nan]) -->nan

np.nansum([1,2,np.nan]) -->3
np.nansum([1,2,None]) -->unsupported operand type(s) for +: ‘int’ and ‘NoneType’

b)All can be used as key in dict;pandas Series/dataframe. But the same & different always exist
None == np.nan -->False
{None:1, np.nan:2} -->None != NaN

s = pd.Series([None, np.nan, 'b']) -->None != NaN

pd.Series([1, None])
=== pd.Series([1, None]) -->True False
s.map({None:1,'b':'a'}) -->None == NaN
0 1
1 1
2 a
dtype: object

s.map({np.nan:2,'None':1,'b':'a'}) -->Nan take into effection 1st
0 2
1 2
2 a
dtype: object

c)np.nan & None impact on other type
np.array([1, None]).dtype -->dtype('O')
np.array([1, np.nan]).dtype -->dtype('float64')

How to convert from np.nan to None
s = pd.Series([None,np.nan,'a'])

s.replace([np.nan],None) -->Seriers([None,None,'a'])
s.replace(np.nan,None) -->Seriers([None,None,'a'])

s.where(s.notnull(), None) -->Seriers([None,None,'a'])



...全文
157 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
屎克螂 2017-03-01
  • 打赏
  • 举报
回复

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧