Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

`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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

min in Python operates on scalar values, not collections of values. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading