pandas/repeat-rows-by-index
| Rule | repeat-rows-by-index |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
Repeating rows through index.repeat leaves a repeated index to reset and no column for the position. Use a cross merge with a frame of positions.
long = keys.loc[keys.index.repeat(POSITIONS)].reset_index(drop=True)long["position"] = np.tile(np.arange(POSITIONS), len(keys))positions = pd.DataFrame({"position": np.arange(POSITIONS)})long = keys.merge(positions, how="cross")What the rule reports
Section titled “What the rule reports”The rule reports frame.loc[frame.index.repeat(n)], and frame.iloc[np.repeat(...)].
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
The repeat copies every row through the index, and the lines after it reset the index and tile a column for the position inside each copy. A cross merge pairs every row with every position in one call. On pandas 3.1.1 the two forms gave equal frames.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This repeats every row of $FRAME through its index. Use a cross merge, as in $FRAME.merge(positions, how="cross") with positions = pd.DataFrame({"position": range(n)}), which also adds the position and leaves no index to reset.Browse the other rules of the pandas pack.