pandas/date-range-slice
| Rule | date-range-slice |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Slicing off the last step of date_range with [:-1] does what inclusive="left" does. Pass inclusive="left" instead.
steps = pd.date_range("2024-03-30", "2024-04-01", freq="15min")[:-1]steps = pd.date_range("2024-03-30", "2024-04-01", freq="15min", inclusive="left")What the rule reports
Section titled “What the rule reports”The rule reports pd.date_range(start, end, freq="15min")[:-1], which drops the end with a slice where inclusive="left" leaves it out. The rule leaves a range with periods= alone.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes pd.date_range(start, end, freq="15min", inclusive="left").
The fix is unsafe when the end isn't a step of the range. Then the slice drops the last real step, and inclusive="left" keeps it. The fix adds an argument after the last one, so the rule leaves a call alone that ends in a trailing comma or holds a comment.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
date_range takes inclusive="left", which leaves out the end point. Write $PD.date_range($$$A, inclusive="left").Browse the other rules of the pandas pack.