Get startedGet started for free

Introduction to errors

1. Introduction to errors

For the remainder of the course, we'll discuss how to handle errors, extending our knowledge of implementing custom logic in our functions. To start, let's discuss what an error is and the important skill of reading error messages.

2. What is an error?

An error occurs when we try to run code that violates a rule. Python has many different types of errors. We may also hear errors called exceptions—these terms have the same meaning. An error will cause our program to terminate, so our code should try to avoid its occurrence where possible!

3. TypeError

Let's look at a couple of common errors. First is TypeError, which generally occurs when we try to use an incompatible data type when performing a task. Here, we try to add a string and an integer and get an error message that this action is not supported on these two data types.

4. ValueError

There is ValueError, which occurs when a value provided is not within an acceptable range. For example, we get a ValueError if we try to convert the string "Hello" to a float. This differs from a TypeError, because no error is produced if we call float and provide the number two as a string.

5. Tracebacks

Notice that the error outputs mentions the word "Traceback"? A traceback can be thought of as a report,

6. Tracebacks

providing information including what type of error occurred, in this case a ValueError,

7. Tracebacks

and where it occurred in our code, in line 1 here.

8. Code in packages

We've looked at errors in our code, but what if an error occurs when using a package? Remember, when we import packages and use their functions, we use somebody else's code. The code for these packages is generally referred to as source code. When we install a package, we download the source code so that it works in our local environment, such as our computer. We don't see it when we call a function from the package, such as the pandas pd.read_csv function, but the source code gets executed behind the scenes.

9. Tracebacks from packages

Let's examine a traceback from using a package. Here, we import pandas, create a DataFrame, and then try to access a column called tag, which doesn't exist.

10. Tracebacks from packages

That's a huge traceback! Let's break it down.

11. Tracebacks from packages

The first few lines show that the error occurred in line 3803 of the code. But we only wrote eight lines. What's going on? We see a line starting with a file location. This refers to the fact that line 3803 is in a file called base.py. Put another way, the pandas' file base.py was executed when we tried to subset our DataFrame for the tag column, and an error occurred in line 3803 of that file. At the bottom, we see it was a KeyError and the following code caused this.

12. Tracebacks from packages

It then shows our code, highlighting the line that caused the error.

13. Tracebacks from packages

After this, we see that the code in frame.py was executed, where it tried to access the key or column name. Lastly, in base.py, the code says raise KeyError, which is where pandas developers have intentionally returned an error because the key does not exist. We'll discuss this syntax more in the following video.

14. Let's practice!

For now, let's test your knowledge of errors.