Working With AI on Libraries You Are Still Learning
Learning Objectives
- You can explain why AI assistants are especially prone to confidently-wrong code about specific libraries.
- You can treat an AI-generated pandas or NumPy call as a hypothesis that needs checking.
- You can verify a library claim with documentation, the installed library, or a tiny local experiment.
- You can write a precise library-help prompt that names the input shape, output shape, and edge cases.
This is the AI-collaboration chapter for the part. The previous AI chapters have focused on reading code, checking behavior, specifying what you want, detecting plausible-but-wrong code, comparing approaches, and critiquing data designs. This part adds the seventh skill: working with AI on a library you are still learning.
That situation is different from asking for help with a loop or a small function. With ordinary Python, you often know enough to spot a made-up keyword or a missing branch by reading. With a library like pandas, the code can look perfectly plausible while depending on details you have not memorized yet: exact method names, exact argument names, column names, data types, and return shapes.
AI assistants are useful in exactly this situation. They can suggest where to look, show common patterns, and help you turn a manual loop into a pandas summary. But they are also easy to over-trust, because the code may look more advanced than your current understanding. The skill is not to avoid AI. The skill is to use it while keeping the final check anchored in the library, your data, and your tests.
Why Libraries Are a Special Trap
An AI assistant has seen many examples of Python code. That helps it produce code with the right general shape. But a library has exact details that cannot be guessed from general Python style.
For pandas, the overall idea may be right:
# group sessions by subject and total the minutesBut a specific line may still be wrong:
totals = df.group_by("subject")["minutes"].sum()That line is plausible. The words make sense. But pandas calls the method groupby, not group_by.
Other mistakes are less obvious:
top = df.sort("minutes").head(3)Pandas uses sort_values(...) for this job:
top = df.sort_values("minutes", ascending=False).head(3)The assistant may also produce code that uses the wrong data shape. In this course, the study-log DataFrame has exactly these columns:
subject, minutes, dateIf an AI answer uses "duration", "topic", or "day", that does not match your program, even if it would work for some other example.
df.groupby("topic")["duration"].sum()That is not a pandas idea you should paste. It is an example written for a different table. Your job is to adapt the library idea to the data you actually have:
df.groupby("subject")["minutes"].sum()Plausible pandas Mistakes
Here are common mistakes to watch for in AI-generated pandas code:
| Mistake | What to check |
|---|---|
| Invented or near-right method name | Does the method exist on your installed pandas object? |
| Wrong column names | Does the code use your real columns: subject, minutes, and date? |
| Wrong return shape | Does the function return the plain Python value the rest of your program expects? |
| Dense method chain | Can you inspect each intermediate result? |
and or or with pandas masks | Should the code use & or ` |
| Dates compared as strings | Has the date column been converted with pd.to_datetime? |
| Missing setup step | Has pandas or NumPy been installed into the same Python environment that runs the program? |
None of these mean the whole answer is useless. Often, the assistant has the right direction and one wrong detail. The important habit is to identify the detail before it enters your project.
Libraries change over time. A function may be renamed, an argument may change, or an example may rely on a version you do not have. AI assistants learn from code written across many versions, so they may mix details from different moments in a library’s history.
This is another reason to ask your installed library what is true. If an AI answer claims a method exists and Python says it does not, trust the evidence from the library and its current documentation.
How to Check a Library Claim
When an AI assistant gives you an unfamiliar pandas line, treat the line as a hypothesis. Check it before building on it.
There are three reliable checks.
First, inspect your data:
print(df.columns)print(df.dtypes)print(df.head())These lines answer three basic questions: what are the column names, what types did pandas infer, and what do the first rows look like? Many pandas bugs are not really pandas bugs. They are mismatches between the code’s assumptions and the actual table.
Second, ask Python about the library:
print(hasattr(df, "groupby"))print(hasattr(df, "group_by"))help(df.groupby)You do not need to read every line that help(...) prints. You are checking whether the name exists and whether the call roughly matches the thing you are trying to do.
Third, run the smallest experiment that tests the doubt:
import pandas as pd
records = [ {"subject": "math", "minutes": 45, "date": "2026-05-10"}, {"subject": "history", "minutes": 30, "date": "2026-05-11"}, {"subject": "math", "minutes": 60, "date": "2026-05-12"},]
df = pd.DataFrame(records)totals = df.groupby("subject")["minutes"].sum()print(totals)print(totals.to_dict())The expected dictionary is:
{"history": 30, "math": 105}If the tiny experiment gives the expected result, you have evidence that the pandas operation does what you think. If it fails, the failure is small enough to inspect.
A Worked Check
Suppose you ask an assistant for a helper that returns total minutes by subject. It gives you this:
def totals_by_subject(df): return df.group_by("subject").sum()["minutes"]The intention is good. The implementation has three things to inspect.
First, check the method name. Pandas uses groupby, not group_by:
grouped = df.groupby("subject")Second, select the column before summing. That keeps the operation focused on the minutes column:
minutes_by_subject = grouped["minutes"].sum()Third, decide the return shape. The rest of the study-log project usually works best with plain Python values at the boundary. A pandas Series is fine inside the helper, but a dictionary is easier to print and test:
return minutes_by_subject.to_dict()The checked version is:
def totals_by_subject(df): grouped = df.groupby("subject") minutes_by_subject = grouped["minutes"].sum() return minutes_by_subject.to_dict()That is a little longer than the one-line version:
def totals_by_subject(df): return df.groupby("subject")["minutes"].sum().to_dict()Both versions can be correct. While you are learning, the named intermediate variables are useful because they give you places to print and inspect:
print(minutes_by_subject)print(minutes_by_subject.to_dict())If something looks wrong, you know which layer to check.
Asking for Small, Checkable Help
The quality of the AI answer depends partly on the prompt. A vague prompt invites guesses:
Make my study log use pandas.That is too broad. The assistant has to guess your data shape, your project structure, your return values, and your edge cases.
A better prompt names the exact helper:
I have a pandas DataFrame called df with columns subject,minutes, and date. The minutes column contains integers.Write a function totals_by_subject(df) that returns aplain Python dictionary mapping each subject to totalminutes. For an empty DataFrame, it should return {}.Use readable intermediate variables, not a dense methodchain.This prompt does several useful things:
- It names the input shape.
- It names the output shape.
- It states the empty case.
- It asks for readable code.
- It limits the answer to one helper.
You can use the same pattern for the other project helpers:
I have a pandas DataFrame called df with columns subject,minutes, and date. The date column has already beenconverted with pd.to_datetime. Write a functionrecent_sessions(df, today_text) that returns a DataFramecontaining sessions from today and the previous six days.Use pd.Timestamp for today_text.When the answer comes back, do not paste it into the full project first. Put it next to a tiny DataFrame and a few assertions. Once it passes the tiny check, integrate it into the study log.
Explain checking AI-generated pandas code
0 / 10 points
In a short explanation (about 4-6 sentences), explain how you should handle AI-generated pandas code for the study log when it looks plausible but uses an unfamiliar method or column name.
Mention the expected DataFrame columns, at least two reliable ways to check the code, and why the final helper should return the shape your project expects.
Your Responsibility at the Boundary
Using a library does not remove design decisions. It changes where they happen.
Pandas can calculate an average. Your program still decides what the average helper returns for an empty DataFrame.
Pandas can convert dates. Your program still decides where date strings become date values.
Pandas can sort rows. Your program still decides whether the helper returns a DataFrame, a list of dictionaries, or one dictionary.
An AI assistant can suggest any of those choices. It may even explain them well. But your project has a shape already: Session objects, a StudyLog, JSON loading and saving, and dashboard helpers. The assistant does not automatically know which boundaries the course expects you to keep.
A good rule for this part is:
- Use AI for one helper at a time.
- Tell it the exact DataFrame columns and expected return value.
- Ask for readable intermediate variables.
- Test the helper on a tiny DataFrame.
- Only then connect it to the project.
That is the library version of the whole AI thread so far. Read the code. Check the behavior. Specify what you want. Watch for plausible wrong details. Compare approaches. Critique the data shape. Then use the library deliberately.
Where You Are in the Arc
The course’s AI-collaboration skills build on one another:
| Course section | AI skill | What it gives you |
|---|---|---|
| Getting Started with Programming | Reading code you didn’t write | Knowing what a piece of code does |
| Making Decisions and Repeating Work | Verifying behavior and finding edge cases | Knowing whether what it does is right |
| Organizing Code with Functions | Specifying what you want | Knowing what to ask for in the first place |
| Reading and Writing Files | Detecting plausible-but-wrong code | Knowing when “it runs” is not “it works” |
| Summarizing Your Data | Comparing approaches | Knowing how to choose between several right answers |
| Structuring Your Data | Critiquing data designs | Knowing whether the data is in the right shape |
| Using Libraries | Working with AI on libraries you’re learning | Knowing how to verify unfamiliar library code |
| Data Visualization | Critiquing AI-suggested visualizations | Knowing when a chart suggestion serves the data |
| Games and Programming in the Wild | Iterating with AI on a real project | Knowing how to use AI without losing control of the finished work |
Check Your Understanding
- Why can AI-generated pandas code look plausible even when a specific method name is wrong?
- A helper uses
df.groupby("topic")["duration"].sum(), but your DataFrame hassubject,minutes, anddate. What needs to be changed before the code matches your data? - Name three ways to check whether an unfamiliar pandas call is real and appropriate.
- Why is it useful to ask an assistant for readable intermediate variables instead of a dense method chain?
- What should a good prompt say when asking for a pandas helper for the study log?
AI Study Aid
Create a chapter diagram
We're looking into to what extent AI-generated diagrams could help with studying.
Use this study aid to generate an AI-generated visual summary of the material you just studied.
Each diagram style emphasizes the content in a different way, so you can choose the focus
that feels most useful.
Using the diagram generator is voluntary and does not affect your course progress. We encourage
you to try it out and see if it helps, but it's totally up to you! Your answers help us understand
how to make better study aids in the future.