python的各种max方法:max,np.nanmax,np.amax

求最大值是算法中比较常见的问题,python中有多种求最大值的方法,而且对于不同的对象,适用方法不一样。

1 三种不同的对象:pd.Series,np.array,list

不为空的情况

s1 = pd.Series([1, 2, 3, np.nan])
a1 = pd.Series([1, 2, 3, np.nan]).values
l1 = [1, 2, 3, np.nan]
print(type(s1))
print(type(a1))
print(type(l1))
# ===================================================
[Out]:
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>
<class 'list'>

为空的情况

s2 = pd.Series([])
a2 = pd.Series([]).values
l2 = []

2 xxx.max()方法

对于非空对象,Series返回最大的数值,array会返回nan,list则会报错

s1_max_1 = s1.max()
print(s1_max_1)
a1_max_1 = a1.max()
print(a1_max_1)
l1_max_1 = l1.max()
print(l1_max_1)
# ===================================================
[Out]:
3.0
nan
AttributeError: 'list' object has no attribute 'max'

对于空对象,Series返回nan,array和list则会报错

s2_max_1 = s2.max()
print(s2_max_1)
a2_max_1 = a2.max()
print(a2_max_1)
l2_max_1 = l2.max()
print(l2_max_1)
# ===================================================
[Out]:
nan
ValueError: zero-size array to reduction operation maximum which has no identity
AttributeError: 'list' object has no attribute 'max'

2 max(xxx)方法

对于非空对象,各类对象都是返回最大的数值

s1_max_2 = max(s1)
print(s1_max_2)
a1_max_2 = max(a1)
print(a1_max_2)
l1_max_2 = max(l1)
print(l1_max_2)
# ===================================================
[Out]:
3.0
3.0
3

对于空对象,各类对象都会报错

s2_max_2 = max(s2)
print(s2_max_2)
a2_max_2 = max(a2)
print(a2_max_2)
l2_max_2 = max(l2)
print(l2_max_2)
# ===================================================
[Out]:
ValueError: max() arg is an empty sequence
ValueError: max() arg is an empty sequence
ValueError: max() arg is an empty sequence

3 np.nanmax(xxx)方法

对于非空对象,各类对象都是返回最大的数值

s1_max_3 = np.nanmax(s1)
print(s1_max_3)
a1_max_3 = np.nanmax(a1)
print(a1_max_3)
l1_max_3 = np.nanmax(l1)
print(l1_max_3)
# ===================================================
[Out]:
3.0
3.0
3.0

对于空对象,各类对象都会报错

s2_max_3 = np.nanmax(s2)
print(s2_max_3)
a2_max_3 = np.nanmax(a2)
print(a2_max_3)
l2_max_3 = np.nanmax(l2)
print(l2_max_3)
# ===================================================
[Out]:
ValueError: zero-size array to reduction operation maximum which has no identity
ValueError: zero-size array to reduction operation fmax which has no identity
ValueError: zero-size array to reduction operation maximum which has no identity

4 np.amax(xxx)方法

对于非空对象,Series返回最大的数值,array和list会返回nan

s1_max_4 = np.amax(s1)
print(s1_max_4)
a1_max_4 = np.amax(a1)
print(a1_max_4)
l1_max_4 = np.amax(l1)
print(l1_max_4)
# ===================================================
[Out]:
3.0
nan
nan

对于空对象,Series返回nan,array和list会报错

s2_max_4 = np.amax(s2)
print(s2_max_4)
a2_max_4 = np.amax(a2)
print(a2_max_4)
l2_max_4 = np.amax(l2)
print(l2_max_4)

# ===================================================
[Out]:
nan
ValueError: zero-size array to reduction operation maximum which has no identity
ValueError: zero-size array to reduction operation maximum which has no identity

5 总结

  1. 对于空对象的array和list,各种方法都会报错,所以需要加if条件判断,避免报错;
  2. 对于空对象的Series,只能采用.max()和np.amax(),会返回nan;
  3. 对于非空对象,建议都转成Series再求最大值,因为各个方法都是准确的;
  4. 对于非空的各类对象,max()和np.nanmax()计算的结果都是准确的;
  5. 对于非空对象,如果有nan值,.max和np.amax()会返回nan,这是个坑,需要避免;
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值