pythonmathmin

`min()` is not working properly in Python?


I am new to Python and notice the following error when using min().

import pandas as pd
a = [1,2,3,4,5]
print(min(20, pd.array(a)**2))

The code should return[1, 4, 9, 16, 20], however, it returns [1, 4, 9, 16, 25]. It seems that min() is not working as expected. What is the reason and how can I fix it? Thank you.


Solution

  • min in Python doesn't support the bulk element-wise operations supported by pandas/numpy arrays; it returns a single minimum value (which might be a collection of multiple values, but that collection would be one of the inputs, not a newly constructed thing created programmatically in response to the comparison).

    That said, pandas is built on a base of numpy, and numpy does offer an element-wise minimum function of this sort, so by mixing pandas and numpy you can get what you expect:

    import pandas as pd
    import numpy as np  # Get numpy as well
    
    a = [1,2,3,4,5]
    
    # Creates a pandas array, but gets element-wise minimums via numpy.minimum
    print(np.minimum(20, pd.array(a)**2))
    

    which, on the W3Schools pandas interpreter I just loaded to verify, outputs:

    <IntegerArray>
    [1, 4, 9, 16, 20]
    Length: 5, dtype: Int64
    

    Somewhat surprisingly, but conveniently, this even returns a new pandas array, not a numpy array; the integration between the two of them is deep enough that you keep your expected types even through the non-pandas function.