1

I use the following code to get a row from a dataframe and then find the max value.

def find_max(a):
    return a.values.max()

row = df.iloc[0].astype(int)
max_value = find_max(a)

That works fine. However, if I pass an array like

ar = [1,2,3]
max_value = find_max(ar)

it doesn't work and I receive AttributeError: 'list' object has no attribute 'values'. How can I use that function for both types?

2
  • something along the lines of isinstance dataframe else max()? Commented Jan 26, 2022 at 13:24
  • 2
    Note: a Python list is not generally referred to as an array. Pandas builds on top of numpy, which does have array type. You could use numpy.max(), and test if .values exists. Commented Jan 26, 2022 at 13:24

3 Answers 3

6

You'll have to test for the type, or use exception handling, to switch approaches:

def find_max(a):
    if isinstance(a, list):
        return max(list)
    return a.values.max()

or

def find_max(a):
    try:
        return a.values.max()
    except AttributeError:
        # assume a Python sequence
        return max(a)

Which one you pick depends on a few factors:

  • Do you need to support other iterables, like tuples or sets or generators?
  • What is the more common case; the try...except case is faster if the majority of calls pass in a Pandas series.

Another option is to use np.max(), which works on either type:

import numpy as np

def find_max(a):
    return np.max(a)

Note that there is no need to use the .values attribute here!

Sign up to request clarification or add additional context in comments.

4 Comments

I would have actually checked for a dataframe type instead of a list, as max "covers" everything but that, but I like the EAFP approach better anyway
@Bharel: it depends. What other types are you passing in?
I'm not the OP, but considering .values.max() will work only for specific types while max is for general iterables, I'd special case the first.
@Bharel: I meant the general 'you' more than the specific. :-)
5
def find_max(a):
    if isinstance(a, list):
        return max(a)
    elif isinstance(a, pd.DataFrame):
        return a.values.max()
    else:
        print("No max method found for the input type")

3 Comments

I assumed that DataFrame is the default input type. How would you improve it?
Its fine hence the edit and upvote - try: except: would work for any sequence but it is not needed
@PatrickArtner Thanks. How does a try-except block compare to my current (edited) solution? Would that be better?
4

Why don't try numpy's integration for that?

import numpy as np

row = df.iloc[0].astype(int)
max_value = np.max(a.values)

ar = [1,2,3]
max_value = np.max(ar)

This way, you don't have to define a function. In general it`s more pythonic to use already existing and well tested functionality from python packages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.