Turn Messy Text into Structured Insights With AI. Join Greg Live:Β Dec 4th, 2024
Leverage
Pandas errors

TypeError Pandas Missing Argument – How to fix

Pandas TypeError explanation and resolution

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 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()

Link to code

On this page