TypeError Pandas Missing Argument – How to fix

Pandas TypeError is frustrating. This error happens because pandas was expecting an argument but it couldn’t find one. Luckily, Pandas tells you what it was looking for but couldn’t find

How to Fix:

  1. Check the error output of your pandas function to see which argument it was expecting to see
  2. Make sure to add that argument to your pandas function
1. df.sort_values() >> This would throw an error because the 'by' argument is required
2. df.sort_values(by='your_column') >> This would work!

Pseudo code: write out and include all required functions

Pandas TypeError

Pandas TypeError and how to fix

Pandas throws this error because it can not find a positional argument it was expecting in your function.

A positional argument will not have a default value and is required for your function to run.

You will run into this error when you are running a pandas script or jupyter notebook cell block. It will stop your program from running (unless you have error handling in place).

The best place to learn which arguments are required is to go to dataindependent.com and search for your function to learn more. The second best place to look is on the official pandas documentation website.

From there you will see which arguments have default values and which don’t. The ones that are required won’t have an assignment (an equals sign) with a value after them.

To be safe, I like to add all of my positional arguments in my function explicitly. This means I write them all out in my function so I can see exactly which arguments have which values.

This may seem a bit verbose, but it helps with clarity, helping others read your code, and helping you remember what code you wrote when you come back to it.

Here is a mini-example of explicit vs not.

1. df.sort_values('name') >> not explicit
2. df.sort_values(by='name') >> explicit

Let’s take a look as an example below using pandas sort_values()

Pandas Type Error Missing Argument

You might see the following error in your code: TypeError: {any_fucntion} missing 1 required positional argument: 'by'

Pandas TypeError can be annoying, especially when you miss an arguement. This means that a pandas function is expecting an arguement, but it did not find one.

Let's check out some examples:

  1. Invoking the error
  2. Fixing by being explicit about all arguments

First let's create our DataFrame

In [1]:
import pandas as pd
In [3]:
df = pd.DataFrame([('Foreign Cinema', 'Restaurant'),
                   ('Liho Liho', 'Restaurant'),
                   ('500 Club', 'bar'),
                   ('The Square', 'bar')],
           columns=('name', 'type')

0Foreign CinemaRestaurant
1Liho LihoRestaurant
2500 Clubbar
3The Squarebar

Great, now that we've done that, let's call sort_values() on this DataFrame.

In [4]:
TypeError                                 Traceback (most recent call last)
<ipython-input-4-9baf0daa5fc6> in <module>
----> 1 df.sort_values()

TypeError: sort_values() missing 1 required positional argument: 'by'

Oh no! We get a type error. Let's investigate this one more.

You see how the pandas error mentions sort_values()? This means that there is a problem with this function.

missing 1 required positional argument: 'by' means that pandas thought there was going to be an argument by but it could not find it.

sort_values() takes an argument by which tells pandas what to sort the values of.

Let's try adding this one to our code and see if we get the solution. I'll be explicit about the argument and put by= in my function.

In [9]:
2500 Clubbar
0Foreign CinemaRestaurant
1Liho LihoRestaurant
3The Squarebar

That was easy.

An alternative would have been to just do

In [10]:
2500 Clubbar
0Foreign CinemaRestaurant
1Liho LihoRestaurant
3The Squarebar

Since by is a positional argument, this means that what ever you put first will be sorted. Since there is only one arguement this works out well!

Bonus: Let's reverse the order

In [11]:
df.sort_values('name', ascending=False)
3The Squarebar
1Liho LihoRestaurant
0Foreign CinemaRestaurant
2500 Clubbar

Here I added one more optional argument of ascending which tells pandas which order to sort in

Link to code above

Check out more Pandas functions on our Pandas Page