Skip to content

pandas

The pandas pack catches broken pandas 3 copy-on-write code, slow Python row loops, index alignment traps, and verbose chains on files that import pandas. House style rules under Method chains report as info by default and as warning when style = "strict" is configured in xpd check.

Rules Safe fix Unsafe fix Manual fix
140 5 94 41
Rule Severity Fix What it finds
chained-assignment error unsafe Under copy-on-write, assigning through a chain such as df["a"][i] = v writes to a copy, so the frame keeps its old values.
inplace-on-column error unsafe Under copy-on-write, an inplace=True call on a selected column changes a copy, so the frame keeps its old values.
inplace-result-assigned error unsafe A call with inplace=True returns None, so df = df.dropna(inplace=True) sets df to None.
no-inplace info unsafe inplace=True gives no speedup and keeps the call out of a method chain.
epoch-from-int-cast error unsafe pandas 3 stores datetimes in microseconds by default, so casting a datetime to an int and dividing by 10**9 is off by a factor of 1,000.
compare-to-missing error unsafe A comparison with a missing value, such as df["b"] == np.nan, is never true.
in-column error unsafe The in operator on a column looks among the index labels, not the values, so 2 in df["a"] can be wrong.
mask-precedence error unsafe Python applies & and | before comparisons, so df["a"] > 1 & df["b"] < 3 compares against the wrong value.
write-through-values error unsafe Under copy-on-write, the array behind a column is read-only, so writing through .values raises ValueError.
object-dtype-check error unsafe pandas 3 stores text in the str dtype, so a check for object is False for a text column.
datetime-unit-check error unsafe pandas 3 stores datetimes in microseconds by default, so a check for datetime64[ns] is False for them.
groupby-apply-reads-key error none groupby.apply no longer passes the grouping column to the function, so reading it inside the function raises KeyError.
removed-in-pandas-3 error unsafe These names and arguments were removed in pandas 3, so the code raises.
reduction-level-argument error unsafe The level argument of reductions such as sum was removed, so the call raises TypeError on pandas 3.
stack-dropna error unsafe stack no longer takes dropna, so the call raises ValueError on pandas 3.
frame-append error unsafe DataFrame.append and Series.append were removed, so the call raises AttributeError on pandas 3.
str-replace-regex-default error unsafe Since pandas 2.0, str.replace reads its pattern as plain text, so a regular expression replaces nothing.
positional-key-on-labels error unsafe On pandas 3, an integer key on a Series is a label, so value_counts()[0] and similar lookups raise KeyError unless 0 is one of the labels.
day-frequency-with-time-zone warning unsafe On pandas 3, "D" means a calendar day, so shifting a time zone aware index by a day can raise at a daylight saving change.
mixed-offsets-without-utc warning unsafe On pandas 3, to_datetime raises ValueError when texts with a UTC offset hold more than one offset, as local times do across a daylight saving change.
deprecated-in-pandas-3 warning unsafe These names and arguments warn on pandas 3 and will stop working in a later release.

Shorter forms, including the pandas 3.0 and 3.1 API

Section titled “Shorter forms, including the pandas 3.0 and 3.1 API”
Rule Severity Fix What it finds
sort-then-reset-index info unsafe reset_index(drop=True) after a sort renumbers the rows in a second step.
set-index-then-resample warning unsafe Setting a column as the index only to resample takes an extra step.
subtract-shift warning unsafe Subtracting a column's own shift() computes the difference from the previous row by hand.
range-mask warning unsafe Two comparisons joined with & state one range twice.
groupby-then-reset-index warning unsafe reset_index() after a groupby moves the keys back into columns in a second step.
rename-after-agg warning unsafe Renaming columns right after agg names them in a second step.
merge-indicator-anti-join warning none Filtering the _merge column after merge(indicator=True) builds an anti join by hand.
value-counts-share warning unsafe Dividing value_counts() by the length computes shares by hand.
nested-np-where warning none Nested np.where calls read from the inside out and build an array for every level.
query-string info none A query string hides column names from editors, type checkers and xpd.
filter-items warning unsafe filter(items=) skips a missing name without an error, and pandas 3.1 plans to deprecate it.
inline-column-list info unsafe A list of three or more column names written inside a call is hard to read and to reuse.
concat-then-category warning unsafe Casting to category after concat makes a second pass over the data.
Rule Severity Fix What it finds
apply-known-function warning unsafe Passing a function such as len, pd.isna or pd.to_datetime to apply calls it once for every value.
literal-contains warning unsafe str.contains treats its pattern as a regular expression by default, even when the pattern is plain text.
copy-after-indexing warning unsafe Under copy-on-write a selection already behaves as a copy, so .copy() after it only costs memory and time.
lookup-per-row warning unsafe Looking up a key in another frame inside apply reads that frame once for every row.
apply-recomputes-aggregate warning unsafe A lambda that computes df["b"].mean() runs that mean again for every row, so the time grows with the square of the row count.
enumerate-map-encoding warning unsafe Numbering values with a dict built from enumerate(unique()) looks up every value in Python, which was 7 to 17 times slower.
copy-in-chain warning unsafe Methods such as rename and assign return a new frame, so a .copy() next to them is thrown away.
row-loop warning none iterrows() builds a Series for every row, and a loop that only computes from row values runs Python once for every row.
apply-axis-1 warning unsafe apply(axis=1) calls a Python function once for every row, which was hundreds of times slower than working on whole columns.
apply-text-method warning unsafe Calling a text method inside apply or map runs Python once for every value.
apply-date-part warning unsafe Reading a date part such as the year inside apply runs Python once for every value.
apply-condition warning unsafe A condition inside apply runs Python once for every value.
apply-arithmetic warning unsafe Arithmetic inside apply runs Python once for every value.
builtin-on-column warning unsafe A Python builtin such as sum, set or list walks a column one value at a time, which was up to 483 times slower.
list-over-column warning unsafe A list comprehension or a loop over a column runs Python once for every value, which was 13 to 19 times slower.
frame-growth-in-loop warning none Adding one row at a time with out.loc[len(out)] = row copies the frame on each pass.
len-of-filter warning unsafe len(df[mask]) builds a filtered frame only to count its rows.
sort-then-head warning unsafe Sorting a whole frame to keep its first or last few rows does more work than needed.
replace-with-mapping warning unsafe replace with a mapping takes longer for every key in the mapping.
concat-in-loop warning none Calling concat inside a loop copies every earlier row on each pass.
apply-math-function warning unsafe Passing a math function such as np.sqrt to apply or map calls it once for every value.
apply-cast warning unsafe Casting with apply(int) or map(float) converts one value at a time in Python.
apply-comparison warning unsafe A comparison inside apply runs Python once for every value.
apply-pd-series warning unsafe apply(pd.Series) builds one Series for every row to split a column of lists or dicts.
index-loop warning none A loop over index labels that reads the frame with loc or at runs Python once for every row.
zip-over-columns warning none A loop over zipped columns runs Python once for every row.
column-loop warning none A loop that reduces one column per pass makes a separate call for every column.
filter-before-frame warning none Filtering records in a Python comprehension before you build a frame tests every record in Python.
Rule Severity Fix What it finds
groupby-lambda warning unsafe A lambda or a NumPy function in agg runs Python once for every group.
groupby-lambda-window warning unsafe A lambda in transform that calls shift, diff or a similar method runs Python once for every group.
groupby-lambda-arithmetic warning unsafe A lambda in transform that divides or subtracts a group total runs Python once for every group.
groupby-filter-lambda warning unsafe groupby(...).filter(lambda g: len(g) > n) runs Python once for every group only to count its rows.
groupby-apply warning unsafe groupby(...).apply with a lambda runs a Python function once for every group.
aggregate-then-merge-back warning none Merging a group total back onto its frame builds a second frame and joins it, when transform already gives each row the total of its group.
named-agg-lambda warning none A lambda inside named aggregation runs Python once for every group.
groupby-loop-collects warning none A loop over groupby that appends one summary for each group builds the result by hand.
groupby-first-skips-missing warning unsafe groupby(...).first() and last() take the first value that is not missing in each column, so one result row can mix values from different rows.
Rule Severity Fix What it finds
column-loop-to-long info none A loop that builds one frame per column and stacks them does what melt does in one call.
stack-then-reset-index info none stack().reset_index() gives a long frame with columns named level_1 and 0.
melt-default-names info none melt without var_name names the column of old column names variable, which says nothing about them.
pivot-table-default-mean warning unsafe pivot_table without aggfunc takes the mean of the rows that share a cell and says nothing, so two rows of 10 and 30 become 20.0.
unstack-without-fill warning unsafe After unstack, a pair without rows becomes a missing value, which turns int64 counts into float64.
Rule Severity Fix What it finds
assign-drops-alignment warning unsafe Assigning .values, .to_numpy() or .tolist() of another frame drops its index, so pandas pairs the rows by position.
concat-columns-unchecked info none concat(axis=1) pairs rows by index label and keeps every label of every frame, so one differing label adds a row of missing values and turns int columns into floats.
merge-on-both-indexes info unsafe A merge on both indexes is a join, but the merge call hides that rows are paired by index label.
merge-asof-unsorted warning unsafe merge_asof raises ValueError unless both frames are sorted by the key, and nothing here sorts them.
Rule Severity Fix What it finds
multiindex-from-tuples warning unsafe MultiIndex.from_tuples with zip or product builds one Python tuple for every row.
level-values-unique warning unsafe get_level_values(level).unique() builds one value for every row before it finds the unique ones.
tuple-slice-on-levels info safe A tuple with slice(None) in .loc is hard to read.
Rule Severity Fix What it finds
rolling-apply warning unsafe rolling(...).apply with a lambda or a NumPy function runs Python once for every window, which was up to 1,031 times slower.
text-time-key warning unsafe Grouping on dates formatted as text turns every date into text first.
object-dates warning unsafe dt.date and dt.time turn a datetime column into a column of Python objects, which is slow and loses the datetime methods.
apply-on-dates warning unsafe Date arithmetic inside apply runs Python once for every date.
strftime-date-part warning unsafe Formatting dates as text and parsing a number back out was 33 to 198 times slower than reading the part.
apply-date-expression warning unsafe Reading a date part inside apply and comparing it runs Python once for every value, and was 22 to 94 times slower.
strftime-round-trip warning unsafe Formatting dates as text and parsing them again to drop the time was 18 to 202 times slower.
index-year-mask warning unsafe Selecting a year with df.index.year == 2023 computes the year of every row.
apply-date-method warning unsafe Calling a date method inside apply runs Python once for every value.
apply-parse-date warning unsafe Parsing dates with strptime inside apply reads one text at a time, and was 24 times slower on a million rows.
timedelta-per-value warning unsafe Building a Timedelta for every value in Python was 1,549 times slower than converting the column.
tz-localize-without-dst warning none tz_localize raises on a time that repeats or is skipped at a daylight saving change unless you say what those times become.
date-range-then-localize warning unsafe Calling tz_localize on a date_range builds the steps in local clock time and raises at a daylight saving change.
to-datetime-then-localize warning unsafe tz_localize("UTC") after to_datetime raises on text that carries an offset.
convert-then-drop-zone warning safe tz_convert("UTC").tz_localize(None) takes two steps to get UTC times without a time zone.
index-plus-offset warning unsafe Adding an offset to the index by hand replaces the index in a separate step.
date-range-slice warning unsafe Slicing off the last step of date_range with [:-1] does what inclusive="left" does.
index-time-mask warning unsafe Selecting rows by index.time or index.hour builds a Python time or an hour for every row.
reindex-to-date-range warning unsafe Reindexing to a date_range from the first to the last time fills in missing steps by hand.
divide-by-shift warning unsafe Dividing a column by its own shift computes the change from the earlier row by hand.
date-part-key warning unsafe Grouping on a year and a month uses two keys for one month and leaves out months with no rows.
row-window-on-uneven-times warning none A rolling window given as a number counts rows, even with on= naming a time column, so on days with gaps it reaches further back than you meant.
integer-lag-on-datetime-index warning none Integer .shift(n), .rolling(n), .diff(n), .pct_change(n), and row-counted .ewm(...) on a DatetimeIndex or datetime-sorted frame count rows rather than calendar time.
Rule Severity Fix What it finds
silent-dtype-coercion warning none Inspect invalid and fractional values before converting a pandas column.
repeated-reset-index warning none A function that calls reset_index three times or more keeps moving keys between the index and the columns.
repeat-rows-by-index warning none Repeating rows through index.repeat leaves a repeated index to reset and no column for the position.
numpy-round-trip warning none A function that takes columns out with to_numpy three times or more and writes arrays back keeps the row order by hand.
frame-rebuilt-from-columns warning none Building a new frame from three or more columns of another frame copies them one at a time and is easy to get out of step.
assign-echo-keywords warning none Computing local variables only to pass them straight into .assign(a=a, b=b, c=c) duplicates every column name across both the assignment lines and the keyword list.
empty-guard warning unsafe The mean, median and similar reductions of an empty Series are already NaN, with no error.
astype-then-to-numpy warning unsafe astype builds a whole Series only for to_numpy to take its values.
merge-without-validate warning none A merge duplicates rows without a warning when a key repeats on the side you expect to be unique.
merge-without-on warning none Without on=, merge joins on every column name the two frames share, so a new shared column silently changes the join.
to-csv-index warning unsafe to_csv writes the index as a first column, which read_csv reads back as Unnamed: 0.
Rule Severity Fix What it finds
lambda-in-assign info safe A lambda that only reads columns, as in assign(b=lambda d: d["a"] * 2), hides a simple expression.
reassign-ladder info none Assigning a frame three or more times in a row from its own methods leaves a half-built frame under the final name after each line.
filter-ladder info none Filtering a frame three or more times in a row, each time into itself, leaves a half-filtered frame under the final name after each line.
frame-column-write info none frame["x"] = ... changes the frame it is called on, so a function that was given the frame changes the frame of its caller.
nested-step-calls info unsafe Three functions called one inside the other read from the inside out, so the first step is written last.
script-steps info none A module that builds its frames in ten or more statements at its top, with no function that takes a frame, runs only from its first line to its last.
Rule Severity Fix What it finds
plot-without-plotly-backend info unsafe A file that draws with df.plot and sets no backend gets matplotlib charts that xpd's plotly rules cannot check.
plot-kind-argument info safe df.plot(kind="bar") hides the kind in a text argument.
matplotlib-on-frame info none Drawing frame columns with matplotlib or seaborn gives a chart that xpd's plotly rules cannot check.
Rule Severity Fix What it finds
read-csv-slow-option warning unsafe engine="python", sep=None and low_memory=False turn off the fast C reader of read_csv, and sep=None was 68 times slower.
read-then-head warning unsafe read_csv(path).head(n) parses the whole file to keep a few rows.
read-csv-converters warning none converters in read_csv calls a Python function once for every value, which was 3.6 to 5.1 times slower.
read-json-lines-engine warning unsafe read_json(lines=True) without an engine parses one line at a time.
to-csv-append-header error unsafe to_csv(mode="a") writes the header again on every call, so header rows end up among the data.
Rule Severity Fix What it finds
csv-round-trip warning none Writing a frame to CSV and reading it back makes pandas guess every dtype again, so dates, categories and nullable integers come back as other types.
pickle-or-hdf-storage info none read_pickle runs whatever code the file tells it to, so a pickle from someone else can run any code.
read-parquet-all-columns warning safe Selecting columns after read_parquet reads every column of the file first.
read-parquet-then-filter info unsafe Filtering rows right after read_parquet reads every row of the file first.
chunked-csv-concat warning unsafe Reading a CSV in chunks and joining every chunk keeps the whole file in memory anyway, and took 3 times as long as one read.
Rule Severity Fix What it finds
missing-pyarrow warning none pyarrow is not installed, so pandas stores text columns as Python strings and the .str methods run slower.
missing-bottleneck warning none bottleneck is not installed, so pandas computes std, var, min, max and median without it.
missing-plotly warning none The project has matplotlib and no plotly, so df.plot draws static matplotlib charts that xpd's plotly rules cannot check.
missing-pandas-stubs warning none The project has a type checker but not pandas-stubs, so calls to pandas are not type checked.

xpd reports a missing package once per check run when the project environment has pandas and meets the conditions for that package. The plotly and pandas-stubs notices depend on which related tools are present. The bottleneck notice depends on a matching call in the checked code, such as a median or standard deviation with missing values skipped. It names the first matching call and the total count.