Debugging Code

LLMs can write Pandas and Matplotlib code quickly—but even small bugs can cause big headaches.

Debugging is a core skill for every data analyst. Whether you’re writing your own code or using AI-generated solutions, you must be able to spot, interpret, and fix errors.

This section will help you:


Reading a Stack Trace

When you run a Python program and something goes wrong, you’ll often see a stack trace—an error message showing where and why the error occurred.

Look for these key parts:

  • Error Type – Tells you the category (e.g. NameError, TypeError, SyntaxError)
  • Error Message – Explains what went wrong
  • Line Number – Shows where the error occurred in your code

Understanding the stack trace helps you go straight to the issue and fix it faster.


Types of Errors

1. Syntax Errors

These happen when the code is not written according to Python’s rules. Common causes:

  • Missing quotes or brackets
  • Incorrect indentation
  • Typing mistakes

Exercise – Fix the Syntax Error

Key points to consider:

  • The string is missing a closing quote.
  • The .head() method is missing parentheses.

2. Semantic Errors

These errors don’t crash your code—but they cause it to behave incorrectly. The logic might be flawed, or the result might not match your intent.

Warning

Semantic errors can lead code which runs fine but produces wrong results. This is why it is important to understand the code yourself, and to validate AI outputs

Exercise – Spot the Semantic Error

Key points to consider:

  • The code calculates the sum, not the mean.
  • It should use .mean() instead of .sum().

Debugging Strategies

Here are simple techniques to help you fix bugs efficiently:

🔍 Read the Error Message

Carefully read the whole error message—it often gives you all the clues you need.

✅ Add Print Statements

Insert print() to check variable values or confirm code sections are running.

🧪 Isolate the Problem

Comment out parts of the code and re-run it to narrow down the bug.

Follow the Error through the Stack

Sometimes an error can stem from code that you have not written. In those cases, the error you receive may display unknown code, but it also displays a trace to where the error happened in the code that you wrote. Carefully trace the error to the code that you know.

🧪 Test with Simple Data

When complex analysis fails, test with the simplest possible data first:

# If a complex aggregation fails on your full dataset
# Test with a tiny sample first:
test_df = pd.DataFrame({
    'category': ['A', 'B', 'A'],
    'value': [10, 20, 30]
})
# Then apply your analysis function to test_df

🔧 Break Down Complex Functions

If a large analysis function has bugs, split it into smaller, testable pieces:

# Instead of one large function that does everything
def analyze_customer_behavior(df):
    # 50 lines of complex analysis logic
    pass

# Break it into smaller functions you can test individually
def filter_active_customers(df): pass
def calculate_customer_metrics(df): pass
def create_customer_segments(df): pass

🤖 Ask the AI

If stuck, share the error message and code with an LLM. Be mindful of data privacy when sharing code snippets (see Practicing Responsible AI).

I'm getting a KeyError on line 6 when trying to filter my DataFrame. Here's my code. Can you help me fix it?

Using VS Code to Debug

If you’re using Visual Studio Code, try the built-in debugger. It allows you to:

  • Set breakpoints to pause your code at specific lines
  • Inspect variables at each step
  • Step through code line by line to watch what happens
Tip

To start debugging in VS Code:

  1. Click on the Run and Debug icon.
  2. Select Python as the environment.
  3. Add breakpoints by clicking next to the line numbers.

Final Debugging Examples

Now here are two examples - apply what has been taught here to these problems to resolve the issues.

Example 1: Syntax Error

Example 2: Semantic Error

Goal: Create a bar chart showing the average rainfall by location and identify which location has the highest average rainfall.


Summary

  • Syntax Errors stop your code from running—check brackets, colons, indentation.
  • Semantic Errors run but give incorrect results—check your logic.
  • Use AI or a debugger to trace the issue step by step.
  • Read error messages carefully—they usually point you in the right direction.
  • Practice spotting bugs—the more you debug, the better you get!