Common Python Errors: Understanding What they mean, and how to fix them
Some Common Python programming errors you will encounter, what they mean, and also how to fix them.
I will create a version of this R soon, and we will also delve into some of the popular libraries such as sklearn, h2o (R), pandas, numpy, etc…
Table of Contents
NameError
TypeError
ValueError
ZeroDivisionError
ModuleNotFoundError
1 - NameError
What it means
A NameError in Python indicates that a given name is not found in the global namespace. This can happen when trying to reference a variable that hasn't been defined, or when trying to call a function that doesn't exist.
NameError can be caused by simple typo mistakes, so it's important to pay attention to your code and look for any potential errors. Sometimes it can be helpful to run your code in a debugger so you can see exactly where the NameError is happening and fix it accordingly.
Most likely scenario
The most likely scenario that will cause you to get a name error when working in data science is you tried to call on a dataframe variable such as df, all_data, new_data, new_df, etc… but you forgot to define it first. Or, you may have defined it, but the odds are there’s a typo somewhere, or you most likely make a character somewhere in there in uppercase.
How to fix it
Take a look at which line the error occurs at, and if you are using pycharm, do a quick Ctrl+F and confirm if you’ve already created the variable it’s talking about, or if there is a typo in there somewhere. This one’s a pretty easy one to fix.
2 - TypeError
What it means
A TypeError most commonly occurs when you try to use a value with an incompatible data type. For example, you may try to add a string and an integer together, or use a function that expects a certain data type but is given a different one. Generally speaking, it means that Python is unable to figure out how to perform the operation you are trying to do.
Most likely scenario
The most likely scenario that you will encounter this error is if you try to pass the wrong type of data to a function. For example, if you use pd.read_csv(), and one of the columns ends up being a date column, but due to erronious data in there somewhere, the entire column could end up being flagged as a string. Then when you try to use time series functions on it, you’ll immediately get a type error, since it expected timedate, but it got string.
How to fix it
There are many ways to fix a TypeError, depending on what your specific code is trying to do. Sometimes you can simply convert the value to the correct data type, or rearrange your code so that it uses compatible types. Other times, you may need to refactor your code completely in order to avoid the TypeError altogether.
3 - ValueError
What it means
A ValueError is an error that is raised when a function encounters a value that it cannot handle. For example, the int() function can only convert strings that represent numbers into integers, so a ValueError is raised if it is called with a string that does not represent a number.
The most common way to generate a ValueError is to input something into a function that it cannot handle. For example, the following code will generate a ValueError because the int() function cannot convert the string "Hello" into an integer.
Most likely scenario
You will most likely encounter this error after you are done fixing your TypeError from above. In this case, you would have finally fixed the data type problem from above, then you’d chuck the data into a function, only to see there’s an outlier here or somewhere, and then voila here’s a new type of error.
How to fix it
You can fix a ValueError in Python by checking the type of the variable and ensuring that it matches the type of the value you're trying to assign to it. For example, if you're trying to assign an integer value to a string variable, you'll get a ValueError.
In most cases, you can also fix a ValueError by making sure that your code follows Python's data typing rules. For example, you should never try to mix strings and numbers in the same calculation, and you should always make sure that all your variables are explicitly defined before you use them.
Hint: Be sure to use the print() function just before the line where you get the error, so you can visually see what it is you are sending.
4 - ZeroDivisionError
What it means
A ZeroDivisionError in Python 3 is an error that occurs when you divide a number by zero. This can happen when you're trying to calculate a mathematic equation, or if you're trying to divide a large number by a very small one. Either way, it's important to be aware of this error and how to deal with it.
In most cases, if you encounter a ZeroDivisionError, it means that something has gone wrong in your code. Maybe you forgot to put in the required parentheses, or maybe you city included an extra division symbol somewhere. Whatever the case may be, it's important to look through your code and find the mistake so that you can correct it.
Most likely scenario
The most likely time you will see this error is if you are working with classification data. In there, you will probably have classes that start from 0 all the way to whatever number. If you were trying to rescale the classes to their quantitative counterparts, that’s when you’ll probably get this error.
Another time you’ll get this error is if you were rescaling your quantitative data, it could be something like a min/max/median scaler. And then, you tried to divided something by another column, and voila, you’ve divided a number by 0.
How to fix it
There are a few ways to fix this error. First, make sure that the number you're dividing by is not actually zero. If it is, you'll need to choose a different number (or find a way to calculate without division).
You can also try using the Decimal module from the standard library. This module allows you to perform decimal arithmetic without losing precision due to floating point errors. Just be aware that this will slow down your code significantly.
5 - ModuleNotFoundError
What it means
If you're getting a ModuleNotFoundError in Python 3, it means that the module you're trying to import doesn't exist. This can happen for a number of reasons, most commonly either because the module doesn't exist or because it's not on your Python path.
Most likely scenario
There are 3 likely scenarios for this error:
You have a typo in your import statement, and Python is looking for a module that doesn’t even exist
When you did sys.path.insert( ) and pointed to a module on your computer, you most likely pointed it to the wrong directory, and Python is looking for your module in the wrong place
You are trying to import a legit module, but you forgot to install it.
How to fix it
There are a few things that you can do to try and fix a ModuleNotFoundError in Python. First, make sure that you've installed the module properly and that it's in the correct location. Second, make sure that you're using the correct spelling and case for the module name. Third, check to see if the module is being used by another process. And finally, restart your computer. If all of those steps don't work, then you may need to reinstall Python 3.
From someone with little coding background, python errors are fairly intimidating.
Wen R version