求最大值是算法中比较常见的问题,python中有多种求最大值的方法,而且对于不同的对象,适用方法不一样。
不为空的情况
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 = []
对于非空对象,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'
对于非空对象,各类对象都是返回最大的数值
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
对于非空对象,各类对象都是返回最大的数值
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
对于非空对象,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