Pandas interviews rarely ask you to recite the API. They hand you a DataFrame that behaves oddly, a join that returned too many rows or a report that takes ten minutes, and they watch how you reason about it. The questions that separate candidates cluster in a few places: how the index drives alignment, what groupby returns, how merges multiply rows and how dtypes decide memory and missing values.
Many slips come from habits formed on older pandas. Since pandas 3.0 shipped in January 2026, Copy-on-Write is always on, chained assignment never updates the original frame, text columns default to a new str dtype and parsed datetimes default to microsecond resolution. Code that passed review in 2024 can now produce wrong numbers with nothing more than a warning, and interviewers who use pandas daily know it.
The 30 questions below are grouped into five areas, each with a short note on what interviewers look for. Every solution gives the answer, the reason and the trap, with code where code is the clearest proof. Examples target pandas 3.x (3.1 is the current release as of October 2026). After the questions you will find a prep plan, a short self-check quiz and answers to common questions.
Intermediate30 questions
Pandas Interview Questions
Practice questions on the pandas skills interviews test most: indexing and alignment, Copy-on-Write, groupby, merges, reshaping, dtypes, time series and performance, updated for pandas 3.
Data ScientistData AnalystData EngineerMachine Learning EngineerQuantitative AnalystAnalytics Engineer
Interviewers start here because most silent pandas bugs trace back to the index. Arithmetic, column assignment and comparisons all line rows up by label before they touch a value, so a sort, filter or reset in the wrong place changes results without raising anything.
Hold onto one idea: loc works on labels, iloc works on positions, and every operation between two pandas objects aligns on labels first. If you can predict what the index looks like after each step, most questions in this group answer themselves.
Indexing, Alignment and MultiIndex
A teammate filters a DataFrame to rows where country == "US", then writes us.loc[0:2] expecting the first three US rows. They get two rows. Explain what happened and how to select the first three.
EasyIndexing, Alignment and MultiIndex
Sample Answer
Filtering keeps the original row labels, so the US rows here are labelled 0, 2 and 4. loc selects by label and its slices include both endpoints, so us.loc[0:2] returns every surviving label between 0 and 2: rows 0 and 2. iloc selects by position and excludes the stop, like a Python slice, so us.iloc[0:3] returns the first three rows whatever their labels. us.head(3) does the same. The trap is treating an integer index as positions. Once the index has gaps, labels and positions diverge, and reset_index(drop=True) is the explicit way to renumber when you really want 0 to n-1.
Python
1import pandas as pd
23df = pd.DataFrame({"country":["US","CA","US","MX","US"],"v":[1,2,3,4,5]})4us = df[df["country"]=="US"]# labels 0, 2, 45print(us.loc[0:2].index.tolist())# [0, 2]: labels 0..2, inclusive6print(us.iloc[0:3].index.tolist())# [0, 2, 4]: first three positions7print(us.reset_index(drop=True).loc[0:2].index.tolist())# [0, 1, 2]
You compute profit = revenue - cost, where both are Series indexed by region. North shows NaN profit and a South row appears that has no revenue at all. What is going on, and how do you fix it?
MediumIndexing, Alignment and MultiIndex
Sample Answer
Arithmetic between two Series aligns on index labels first. The result's index is the union of both, and any label missing on one side yields NaN. North has revenue but no cost row, South has cost but no revenue, so both come out NaN. If a missing side truly means zero, use revenue.sub(cost, fill_value=0), which fills only labels present on one side. If labels differ by case or whitespace ("West " vs "West"), alignment treats them as different keys, so clean labels before the math. The trap is calling .to_numpy() on both sides to make the NaNs go away. That pairs values by position and subtracts the wrong regions whenever the two orders differ.
Python
1import pandas as pd
23rev = pd.Series({"East":100,"West":80,"North":50})4cost = pd.Series({"West":30,"East":60,"South":10})5print(rev - cost)# North and South are NaN6print(rev.sub(cost, fill_value=0))# East 40, North 50, South -10, West 50
You sort a user table by score, call reset_index(drop=True), then attach df["segment"] = segments, where segments is a Series computed earlier from the unsorted table. Segment labels end up on the wrong users and nothing errors. Why?
MediumIndexing, Alignment and MultiIndex
Sample Answer
Assigning a Series to a column aligns on the index, not on position. After the sort and reset, df is labelled 0 to n-1 in score order, but segments still carries the labels from the unsorted table. Row 0 of df receives whatever user had label 0 before the sort, which is usually a different person. Every label matched, so pandas has nothing to complain about. Safe patterns: compute the column from df itself after the sort, or carry a real key and merge on it. If you truly want positional assignment, assign a NumPy array or list (which skips alignment) and assert the lengths match. This bug hides well because the first rows of a small sample often look right.
This filter raises ValueError: The truth value of a Series is ambiguous: df[df.age > 30 and df.country == "US"]. Fix it and explain the error.
EasyIndexing, Alignment and MultiIndex
Sample Answer
Python's and, or and not call bool() on each operand, and pandas refuses to collapse a whole Series into one True or False. Element-wise logic uses &, | and ~ instead. Those operators bind tighter than comparisons, so each condition needs its own parentheses: df[(df.age > 30) & (df.country == "US")]. Without them, 30 & df.country is evaluated first and the expression fails or builds a wrong mask. For long filters, df.query("age > 30 and country == 'US'") accepts and and or inside its string syntax. isin and between handle list and range checks without long chains of |.
sales.groupby(["region", "month"])["revenue"].sum() returns a Series with a two-level index. Pull every month for West, every region for March, and turn it into a region-by-month table. What goes wrong with label slicing on a MultiIndex you built yourself?
HardIndexing, Alignment and MultiIndex
Sample Answer
s.loc["West"] selects on the outer level and drops it from the result. For an inner level, use s.xs("2026-03", level="month") or s.loc[pd.IndexSlice[:, "2026-03"]]. s.unstack("month", fill_value=0) pivots the inner level into columns. The trap is range slicing on an index that is not lexsorted. A groupby result comes back sorted, but a MultiIndex built with set_index after a concat usually is not, and a slice such as s.loc[("East", "2026-01"):("West", "2026-03")] then raises UnsortedIndexError (key length greater than the lexsort depth). Call sort_index() once before heavy slicing; it also makes lookups faster because pandas can binary search the levels.
You set user_id as the index for fast lookups. In tests df.loc[42] returns a Series; in production it returns a DataFrame and downstream code breaks. Explain.
EasyIndexing, Alignment and MultiIndex
Sample Answer
loc with a scalar label returns a Series when the label is unique and a DataFrame of all matching rows when it repeats. Production data has duplicate user_id values, so the return type changes with the data. Check df.index.is_unique right after set_index and fail loudly if it is False (the verify_integrity= argument of set_index is deprecated in pandas 3.1, and the docs point to is_unique instead). If duplicates are legitimate, select with a list, df.loc[[42]], which always returns a DataFrame. A unique index matters for more than lookups: reindex on duplicate labels raises ValueError: cannot reindex on an axis with duplicate labels.
This area changed more than any other in recent pandas, which is why interviewers bring it up. Since pandas 3.0 (January 2026), Copy-on-Write is the only mode: any DataFrame or Series derived from another behaves as an independent copy, and the SettingWithCopyWarning many people learned to silence is gone.
The rule that matters: a write lands on the object you index directly, in one step. df.loc[mask, "col"] = value updates df. Anything that indexes twice before assigning updates a temporary object, and pandas tells you with a ChainedAssignmentError warning rather than an exception.
Copy-on-Write and Safe Updates
On pandas 3.x this line runs, prints a warning, and the DataFrame is unchanged: df[df["status"] == "test"]["revenue"] = 0. Why, and what is the fix?
EasyCopy-on-Write and Safe Updates
Sample Answer
The first [...] builds a new DataFrame from the boolean mask. Under Copy-on-Write that result behaves as a copy, so the second [...] = 0 writes into a temporary that is immediately thrown away. pandas detects the pattern and emits ChainedAssignmentError, which is a Warning subclass, so the script keeps running with the old values. The fix is one indexing call that names rows and columns together: df.loc[df["status"] == "test", "revenue"] = 0. The same rule covers df["revenue"][0] = 0, which also never updates df now. Before 3.0 the second form sometimes worked, depending on whether pandas happened to return a view, and that unpredictability is what CoW removed.
Python
1import pandas as pd
23df = pd.DataFrame({"status":["live","test","test"],"revenue":[10,20,30]})4df[df["status"]=="test"]["revenue"]=0# ChainedAssignmentError warning5print(df["revenue"].tolist())# [10, 20, 30], unchanged6df.loc[df["status"]=="test","revenue"]=0# one step, updates df7print(df["revenue"].tolist())# [10, 0, 0]
A helper does sub = df[df["region"] == "EU"], then sub["price"] = sub["price"] * 1.2, and returns sub. A reviewer asks whether this modifies the caller's df and whether you need .copy(). Answer for pandas 3.
MediumCopy-on-Write and Safe Updates
Sample Answer
It does not touch the caller's df, and .copy() is not needed. Under Copy-on-Write, sub starts as a lazy copy: pandas may share buffers with df, and only when you write to sub does it copy the affected data first. So sub gets the new prices and df keeps the old ones. In pandas 2.x without CoW, the same code raised SettingWithCopyWarning because the outcome was ambiguous, and people added defensive .copy() calls to silence it. Those calls are now redundant and force an eager copy of the data. The reverse trap: if the goal was to update df, the helper must write to df directly, df.loc[mask, "price"] *= 1.2.
Old code calls df["age"].fillna(0, inplace=True). After upgrading to pandas 3, the NaNs are still there. Explain and rewrite it.
MediumCopy-on-Write and Safe Updates
Sample Answer
df["age"] returns a Series that behaves as a copy, so an in-place method mutates that temporary and df never sees the change. pandas 3 warns with ChainedAssignmentError for this pattern as well. Two rewrites work: reassign the column, df["age"] = df["age"].fillna(0), or call the method on the DataFrame with a per-column mapping, df.fillna({"age": 0}, inplace=True). Reassignment reads more clearly and fits method chains. inplace=True rarely saves memory anyway, since most methods build the result and swap it in. One more 3.0 change: fillna, replace, where, clip and a few others now return the object itself instead of None when inplace=True.
Python
1import numpy as np
2import pandas as pd
34df = pd.DataFrame({"age":[31.0, np.nan,45.0]})5df["age"].fillna(0, inplace=True)# warns, df unchanged6print(df["age"].isna().sum())# 17df["age"]= df["age"].fillna(0)# reassign the column8print(df["age"].isna().sum())# 0
A NumPy routine does arr = df["x"].to_numpy(); arr[arr < 0] = 0, and on pandas 3 it raises ValueError: assignment destination is read-only. Why does pandas do this, and what are your options?
HardCopy-on-Write and Safe Updates
Sample Answer
When a column is backed by a single NumPy array, to_numpy() returns a view of the DataFrame's own memory. Under Copy-on-Write that view is marked read-only, because writing through it would change df, and any lazy copies sharing the buffer, behind pandas' back. Options: ask for a copy with df["x"].to_numpy(copy=True) and assign the result back, or stay in pandas with df["x"] = df["x"].clip(lower=0), which is vectorized anyway. Setting arr.flags.writeable = True works but bypasses CoW and can corrupt other objects sharing the buffer, so keep it for profiled hot paths. A DataFrame made of several arrays already returns a fresh copy from to_numpy(), which is why the error only shows up sometimes.
You are upgrading a large pandas 2.1 codebase to 3.x. How do you find code that relied on views or chained assignment before the upgrade starts producing wrong numbers?
HardCopy-on-Write and Safe Updates
Sample Answer
Upgrade to the last 2.x line first. On pandas 2.2 and 2.3, setting pd.options.mode.copy_on_write = "warn" flags every spot whose behavior will change under CoW, including chained assignment and writes that used to propagate through a view. Run the full test suite in that mode and fix each hit with a single .loc call or an explicit reassignment. On 2.3, also set pd.options.future.infer_string = True to preview the str dtype, since checks like dtype == object stop matching text columns in 3.0. Once on 3.x, the mode.copy_on_write option is deprecated and does nothing, so the warn mode only helps before the jump. Turn ChainedAssignmentError into a hard failure in CI with a warnings filter.
Most analytics take-homes come down to group, aggregate and compare over time. Interviewers watch whether you know which groupby method keeps the row count, which collapses it, and when you have fallen back to a slow Python loop wearing an apply costume.
The key idea: agg returns one row per group, transform returns one value per original row aligned to the input index, and apply is the general escape hatch you reach for last. Time adds one more rule. A window defined by time ("7D") and a window defined by row count (7) give different answers as soon as days are missing.
GroupBy, Windows and Time Series
Build a per-user report with total revenue, number of orders and average order value, with flat column names ready to export.
EasyGroupBy, Windows and Time Series
Sample Answer
Named aggregation passes new_name=(column, function) pairs to agg and returns flat, readable columns in one pass. Built-in names like "sum" and "mean" run on optimized compiled paths, while a lambda such as lambda s: s.sum() gives the same answer far more slowly when there are many groups. Watch "size" versus "count": size counts rows including NaN, count counts non-null values, and mixing them up is a common source of order counts that don't reconcile. Add .reset_index() when the next step is a merge or a CSV. The older dict-of-lists style produces a two-level column index that you then have to flatten by hand.
For each order, compute its share of that user's total revenue and flag orders above the user's average. Do it without a merge.
EasyGroupBy, Windows and Time Series
Sample Answer
transform runs a group-wise calculation and broadcasts the result back to every row, aligned on the original index. So df.groupby("user")["rev"].transform("sum") has the same length as df, and dividing gives each order's share directly. The same pattern with "mean" gives the flag. The common wrong answer aggregates with agg, then merges back on user. That works, but it costs a second join and risks the cardinality bugs merges bring. Note that transform must return either one value per group or one value per row; returning a differently sized result raises. Grouped shift, cumsum, rank and diff also keep the row count.
df.groupby("store").apply(lambda g: (g["sales"] * g["margin"]).sum()) takes minutes on 5 million rows. Speed it up, and explain what changed about groupby.apply in pandas 3.
MediumGroupBy, Windows and Time Series
Sample Answer
apply calls a Python function once per group and builds a DataFrame for each one, so cost grows with the number of groups. Compute the row-level product as a vectorized column first, then use a built-in reduction: df.assign(profit=df.sales * df.margin).groupby("store")["profit"].sum(). That keeps the per-group work in compiled code. In pandas 3.0, DataFrameGroupBy.apply no longer passes the grouping columns into your function, and include_groups=True raises ValueError. Code that read g["store"] inside the function breaks; use g.name for the group key instead. Interviewers mostly want to hear that apply comes after agg, transform and filter, not before.
A dashboard groups signups by a categorical plan column with categories free, pro and team. After upgrading to pandas 3, the team row (zero signups this week) vanished. What changed, and how do you bring it back?
HardGroupBy, Windows and Time Series
Sample Answer
pandas 3.0 changed the default of observed in groupby from False to True. With observed=True, a categorical grouper yields only categories that actually appear in the data, so a plan with no rows drops out. Pass observed=False to get every category: sum returns 0 for the empty group and mean returns NaN. Alternatively, reindex the result on df["plan"].cat.categories with fill_value=0. The new default exists for a reason. With several categorical keys, observed=False builds the full cartesian product, so two keys with 10,000 categories each mean 100 million mostly empty groups. When a report must show empty buckets, set observed explicitly instead of relying on any default.
Python
1import pandas as pd
23plans = pd.Categorical(["free","pro","free"], categories=["free","pro","team"])4df = pd.DataFrame({"plan": plans,"signups":[3,2,4]})5print(df.groupby("plan")["signups"].sum().to_dict())# no 'team'6print(df.groupby("plan", observed=False)["signups"].sum().to_dict())# team: 0
Return the top 3 products by revenue within each category. Then change it so products tied at third place are all kept.
MediumGroupBy, Windows and Time Series
Sample Answer
For a fixed N, sort once and take the head of each group: df.sort_values("revenue", ascending=False).groupby("category").head(3). That is one sort plus a cheap positional cut, and it keeps every column. To keep ties, rank within each group and filter: rank(method="min", ascending=False) <= 3. With method="min", tied products share the lowest rank, so two products tied for third both stay. method="dense" would also admit the next distinct value after the tie, which answers a different question. groupby().apply(lambda g: g.nlargest(3, "revenue")) is correct but slow across many groups.
Compute each user's trailing 7-day spend from an event table with irregular timestamps. A teammate used rolling(7). What is wrong, and what is the correct version?
MediumGroupBy, Windows and Time Series
Sample Answer
rolling(7) means the last seven rows, which equals seven days only if every user has exactly one row per day. Events are irregular, so use a time-based window, rolling("7D", on="ts"), inside a groupby, after sorting by user and timestamp. A time window covers (t - 7 days, t] by default and needs a monotonic datetime column within each group. Time windows default min_periods to 1, while integer windows default it to the window size, which is why rolling(7) prints NaN for each user's first six rows. For calendar buckets instead of trailing windows, use resample("W") (weeks ending Sunday) or "ME" for month end; pandas 3 rejects the old "M" alias.
Python
1import pandas as pd
23d = pd.DataFrame({"user":["a","a","a","b"],4"ts": pd.to_datetime(["2026-01-01","2026-01-03","2026-01-09","2026-01-02"]),5"amt":[10,20,30,5]}).sort_values(["user","ts"])6spend = d.groupby("user").rolling("7D", on="ts")["amt"].sum()7print(spend.tolist())# [10.0, 30.0, 50.0, 5.0]: Jan 1 drops out of the Jan 9 window
Joins are where analysis numbers go wrong at scale. A merge on a key you assumed was unique multiplies rows, totals inflate, and nothing errors. Interviewers ask about merges because they want to see you check cardinality before you trust a result.
The habit that matters: state the expected relationship (one-to-one, many-to-one) and make pandas enforce it with validate=, then confirm coverage with indicator=True. Reshaping questions test the same discipline, because pivot refuses duplicate keys while pivot_table quietly aggregates them.
Merges, Joins and Reshaping
orders has 1,000,000 rows. After orders.merge(customers, on="customer_id", how="left") you have 1,084,512 rows and revenue is 9% too high. Diagnose it and prevent it from happening again.
MediumMerges, Joins and Reshaping
Sample Answer
A merge returns one output row per matching pair, so a customer_id that appears k times in customers copies each of its orders k times. Some customers have duplicate rows, perhaps from a history table or a double load. Find them with customers["customer_id"].duplicated(keep=False), decide which row is correct (latest updated_at, say) and dedupe before merging. Then lock the expectation in with validate="many_to_one", which raises MergeError when the right-side keys are not unique. Also assert that a left join you expect to preserve rows still has len(orders) rows. The trap is calling drop_duplicates() on the merged output, which hides the symptom and can delete legitimate repeat orders.
Python
1import pandas as pd
23orders = pd.DataFrame({"customer_id":[1,1,2],"amount":[10,20,30]})4customers = pd.DataFrame({"customer_id":[1,1,2],"tier":["gold","gold","basic"]})5print(len(orders.merge(customers, on="customer_id")))# 5 rows, not 36try:7 orders.merge(customers, on="customer_id", how="left", validate="many_to_one")8except pd.errors.MergeError as e:9print("MergeError:",str(e).splitlines()[0])
Find orders whose product is missing from the catalog and catalog products that never sold, using a single merge.
EasyMerges, Joins and Reshaping
Sample Answer
Use an outer merge with indicator=True. pandas adds a categorical _merge column whose values are left_only, right_only and both. Rows marked left_only are orders with unknown products; right_only rows are products with no sales. value_counts() on _merge gives the match rate in one line, which is worth printing after any join in an interview. Pass a string, as in indicator="source", to rename the column. Then select the columns you need from each side, because an outer merge fills the other side's columns with NaN, and integer columns there turn into floats.
Python
1import pandas as pd
23orders = pd.DataFrame({"order_id":[1,2,3],"sku":["A","B","Z"]})4catalog = pd.DataFrame({"sku":["A","B","C"],"name":["mug","pen","cap"]})5m = orders.merge(catalog, on="sku", how="outer", indicator=True)6print(m["_merge"].value_counts().to_dict())# both 2, left_only 1, right_only 17print(m.loc[m["_merge"]=="left_only","order_id"].tolist())# [3.0], now float8print(m.loc[m["_merge"]=="right_only","sku"].tolist())# ['C']
events.merge(users, on="user_id") raises ValueError: You are trying to merge on int64 and str columns. After a quick astype(str) the merge runs but matches only half the rows. What else is wrong?
HardMerges, Joins and Reshaping
Sample Answer
pandas refuses to merge an integer key with a string key, so the first error is honest. Casting with astype(str) fixes the dtype but not the values. String IDs often carry leading zeros ("00123") or whitespace, and a numeric column that ever held NaN was read as float, so astype(str) turns 123 into "123.0". Pick one canonical form. For numeric IDs, use pd.to_numeric(col, errors="coerce").astype("Int64") on both sides and count how many became NA. For true string IDs, keep strings and str.strip() both. Then measure the match rate with indicator=True. The root fix is reading keys with an explicit dtype in read_csv so they never take a float detour.
df.pivot(index="user", columns="day", values="revenue") fails with ValueError: Index contains duplicate entries, cannot reshape. Fix it, and name the default that can bite you in the fix.
MediumMerges, Joins and Reshaping
Sample Answer
pivot only reshapes, so it requires each (index, column) pair to appear once. Duplicates here mean a user placed several orders on one day. pivot_table aggregates the duplicates instead. Its default aggfunc is "mean", so leaving it out gives average order value per cell, a believable but wrong number for a revenue table. Pass aggfunc="sum" and decide whether an empty cell means 0 or unknown before setting fill_value. Going the other way, melt(id_vars=..., var_name=..., value_name=...) turns a wide table back into long form, which is the shape most groupby and plotting code expects.
Each row of posts holds a tags list. Count distinct posts per tag. What traps come with explode?
MediumMerges, Joins and Reshaping
Sample Answer
posts.explode("tags") emits one row per list element and repeats the other columns. It keeps the original index, so exploded rows share labels; call reset_index(drop=True) before anything that assumes unique labels, or later assignments misalign. An empty list and a missing value both become a single row with NaN, and value_counts() drops NaN by default, so untagged posts quietly vanish from the counts. If a post lists the same tag twice, size() counts it twice, so count with groupby("tags")["post_id"].nunique(). If tags arrive as one comma-separated string, run str.split(",") and strip whitespace before exploding.
For each trade, attach the most recent quote for the same symbol at or before the trade time, but ignore quotes older than 3 seconds.
HardMerges, Joins and Reshaping
Sample Answer
This is an as-of join, and pd.merge_asof does it in one pass: on="t", by="sym", direction="backward" (the default) and tolerance=pd.Timedelta("3s"). Backward picks the last quote whose time is at or before the trade; allow_exact_matches=False makes it strictly before. Both frames must be sorted by the on key, or pandas raises ValueError saying the left or right keys must be sorted. The tolerance turns stale matches into NaN rather than silently using a quote from minutes ago. A regular merge on symbol followed by a filter would build every trade and quote pair per symbol first, which explodes on tick data.
Dtypes decide three things interviewers care about: whether values survive a load intact, how missing data behaves in arithmetic, and how much memory a frame takes. pandas 3 moved the defaults here too, with a dedicated str dtype for text and microsecond datetimes.
The idea that ties this area together: pandas is fast when the work stays in typed columns and compiled code, and slow when it drops to one Python object per value. Row-wise apply, object columns and Python loops are the usual suspects when a job runs out of time or memory.
Dtypes, Missing Data and Performance
A column of integer IDs read from CSV comes back as float64, and an ID like 1234567890123456789 changes value. Why, and how do you keep IDs exact?
EasyDtypes, Missing Data and Performance
Sample Answer
NumPy's int64 has no missing value, so a single empty cell makes pandas store the column as float64 with NaN. A float64 has a 53-bit mantissa, so integers above 2^53 (about 9.007e15) cannot all be represented, and large IDs silently round. Use the nullable Int64 dtype (capital I), which keeps a separate mask and shows missing as pd.NA: pass dtype={"id": "Int64"} to read_csv, or dtype_backend="numpy_nullable" for the whole file. dtype_backend="pyarrow" gives int64[pyarrow] with the same behavior. If the column is an identifier you never do arithmetic on, reading it as a string is the safest choice of all.
After upgrading to pandas 3, a validation step if df[c].dtype == object no longer finds text columns, and df.loc[0, "code"] = 101 on a text column now raises. Explain the new string dtype.
MediumDtypes, Missing Data and Performance
Sample Answer
pandas 3.0 infers text as the new str dtype instead of object. It is backed by PyArrow when PyArrow is installed and falls back to NumPy object storage when it is not. It holds only strings or missing values, so assigning an integer raises TypeError, and its missing-value sentinel is NaN, which keeps behavior close to the old object columns. Checks against object stop matching; use pd.api.types.is_string_dtype(df[c]). If you want pd.NA semantics instead, opt into "string". The payoff is memory and speed: Arrow strings sit in one contiguous buffer instead of one Python object per cell, and many .str methods run in compiled code.
Python
1import pandas as pd
23s = pd.Series(["a","b",None])4print(s.dtype, s.dtype ==object, pd.api.types.is_string_dtype(s))# str False True5try:6 s[0]=1017except TypeError as e:8print("TypeError:", e)
A 50-million-row events table has a country column with about 200 distinct values, and the process runs out of memory. Which dtype would you use, and what are the trade-offs?
MediumDtypes, Missing Data and Performance
Sample Answer
Use category. It stores one small integer code per row plus each distinct label once; with 200 categories the codes are int16, two bytes a row. In a local run on 1 million rows of three country codes, memory_usage(deep=True) reported about 51 MB as object, about 10 MB as the pandas 3 Arrow-backed str dtype and about 1 MB as category. Trade-offs: assigning a value outside the categories raises TypeError until you call cat.add_categories; concatenating categoricals with different categories falls back to a plain string dtype unless you use union_categoricals; and a groupby on a categorical follows the observed rules. For high-cardinality columns such as user IDs, the gain mostly disappears.
Rewrite tiering logic that uses df.apply(lambda r: "high" if r.score > 0.7 else ("mid" if r.score > 0.3 else "low"), axis=1) on 10 million rows.
MediumDtypes, Missing Data and Performance
Sample Answer
Row-wise apply builds a Series per row and calls Python each time. Replace it with a vectorized choice. np.select takes a list of boolean conditions and matching values, evaluated in order, with the first true condition winning, so put the strictest condition first. For numeric thresholds, pd.cut with bins and labels is cleaner and returns a categorical. In a local run on 1 million rows, a vectorized column multiply took about 1 ms while the row-wise apply version took a few seconds, a gap of three orders of magnitude. The trap is np.vectorize, which looks vectorized but still loops in Python.
After upgrading to pandas 3, a feature df["ts"].astype("int64") // 10**9 that produced Unix seconds now produces values 1,000 times too small. What changed, and how do you make it unit-proof?
HardDtypes, Missing Data and Performance
Sample Answer
Before 3.0, parsing always produced nanosecond datetime64[ns]. pandas 3 infers the resolution: strings parse to microseconds (datetime64[us]) unless they carry nanosecond digits, and 3.1 extended the microsecond default to numeric input. astype("int64") returns the raw count in the column's own unit, so it now counts microseconds and dividing by 10**9 is off by 1,000. Unit-proof versions: (df["ts"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s"), or df["ts"].dt.as_unit("s").astype("int64"). For tz-aware columns, subtract a UTC timestamp instead. Audit every place a datetime becomes an integer: hashing, model features and exports to systems that expect nanoseconds.
A 12 GB CSV will not load on a 16 GB laptop. What do you change before reaching for Spark?
MediumDtypes, Missing Data and Performance
Sample Answer
Load less and load it smaller. usecols keeps only the columns you need. An explicit dtype map avoids 64-bit defaults: int32 or float32 where ranges allow, category for low-cardinality text. engine="pyarrow" parses with multiple threads, but it does not support chunksize, so if the result still won't fit, use the default C engine with chunksize=1_000_000, aggregate each chunk and combine partial results (sums and counts, never averages of averages). Convert the file to Parquet once; it is columnar, compressed and typed, so later reads pull only the needed columns. Measure with memory_usage(deep=True), since without deep=True text columns are underreported.
A daily revenue report built in pandas shows 0 for a day when the payments feed was down, while the SQL version shows NULL. Explain the difference and make pandas match.
HardDtypes, Missing Data and Performance
Sample Answer
pandas reductions skip missing values by default (skipna=True), and sum has min_count=0, so a group whose values are all NaN sums to 0. SQL's SUM over only NULLs returns NULL. Pass min_count=1 and the all-missing day becomes NaN, which a dashboard can show as missing instead of a fake zero. mean also skips NaN, so it averages over the non-missing rows; calling fillna(0) first changes the denominator and drags the average down. With nullable dtypes, comparisons with pd.NA return NA, and a boolean mask containing NA selects nothing for those rows, which mirrors SQL's WHERE behavior.
Python
1import numpy as np
2import pandas as pd
34df = pd.DataFrame({"day":["mon","tue"],"rev":[np.nan,120.0]})5print(df.groupby("day")["rev"].sum().tolist())# [0.0, 120.0]6print(df.groupby("day")["rev"].sum(min_count=1).tolist())# [nan, 120.0]
Before running a line, say out loud what the index will look like afterwards. Filters keep old labels, reset_index renumbers, groupby moves keys into the index and explode repeats labels. Practise label versus position selection in Select rows with loc and iloc until loc slices including their endpoint feels obvious.
Write every update as one indexing call
Under pandas 3, df.loc[rows, cols] = value is the only update pattern you need. Rewrite any chained assignment or inplace=True on a column the moment you see it, and say why in the interview. Update values safely drills this, and Mutability traps covers the Python reference rules underneath it.
Check cardinality before trusting a join
Make three checks a reflex: duplicated() on the key you expect to be unique, validate= on the merge and indicator=True to measure coverage. Print row counts before and after. Keep matches or keep every search and Join on multiple keys walk through both failure modes on real-looking data.
Know agg, transform and filter cold
Most groupby prompts are one of three shapes: one row per group (agg), one value per original row (transform) or keep whole groups that pass a test (filter). Name the shape before writing code. Practise named aggregation in Build multi-metric reports and row-level context in Add group context to rows.
Practise time windows on irregular data
Build a small event table with missing days and compare rolling(7), rolling("7D") and resample("W") by hand. Then check the dtype of every parsed timestamp, since pandas 3 defaults to microseconds. Calculate rolling metrics and Build daily and weekly reports cover both window styles.
How Ready Are You for Pandas Interviews?
1 / 6
Indexing
After filtering a DataFrame, df.loc[0:2] returns two rows instead of three. What is the best explanation?
Frequently Asked Questions
Is pandas asked in data science interviews?
Yes, often. Many data science and analytics loops include a live coding round in Python where pandas is the expected tool, and take-home assignments almost always arrive as CSV files. Interviewers look for clean groupby and merge logic, awareness of missing data and the ability to explain why a number is right. SQL rounds cover similar ground, so practising both on the same problems is efficient.
Which pandas version should I prepare for?
Prepare for pandas 3.x. Version 3.0 shipped in January 2026 and 3.1 is current as of October 2026. The changes most likely to come up are Copy-on-Write as the only mode, the str dtype for text, microsecond datetime resolution and observed=True as the groupby default. Interviewers may still run 2.x, so knowing what changed and why is a strong signal either way.
How long does it take to prepare for a pandas interview?
If you already use pandas at work, one to two weeks of focused practice is usually enough: an hour a day on selection, groupby, merges, reshaping and time series, plus a few timed mixed problems. Starting from basic Python, plan on four to six weeks. The fastest gains come from writing code without autocomplete and explaining each step aloud.
Should I use pandas or SQL in a live coding interview?
Use what the interviewer asks for, and if you get a choice, use the one you can write correctly fastest. pandas shines for multi-step transformations, time series and quick checks on intermediate results. SQL reads more clearly for set-based joins and aggregations. Many candidates sketch the logic in SQL terms, then implement it in pandas, which is a reasonable way to show structured thinking.
Do I need to know Polars or PySpark as well?
Not for most pandas interviews, but it helps to know where pandas stops. Single-machine pandas handles a few gigabytes comfortably with good dtypes. Beyond that, interviewers expect you to mention Parquet, chunked processing, Polars or Spark. You do not need their APIs memorized; being able to say why a lazy or distributed engine fits a larger job is usually enough.
What level of pandas do I need for a data analyst role?
Analyst interviews expect fluent filtering, sorting, groupby with named aggregation, merges with the right join type, pivot tables and basic date handling. You should be able to spot a row explosion after a join and explain missing values in a result. Copy-on-Write details, MultiIndex slicing and memory tuning appear more often in data scientist and data engineer loops.
Written by
Dan Lee
Data & AI Lead
Dan is a seasoned data scientist and ML coach with 10+ years of experience at Google, PayPal, and startups. He has helped candidates land top-paying roles and offers personalized guidance to accelerate your data career.