pandas/index-time-mask
| Rule | index-time-mask |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Selecting rows by index.time or index.hour builds a Python time or an hour for every row. Use at_time or between_time instead.
noon = ts[ts.index.time == time(12)]working = ts[(ts.index.hour >= 8) & (ts.index.hour < 16)]noon = ts.at_time(time(12))working = ts.between_time("8:00", "16:00", inclusive="left")What the rule reports
Section titled “What the rule reports”The rule reports ts[ts.index.time == time(12)] and ts[(ts.index.hour >= 8) & (ts.index.hour < 16)], which build a Python time or an hour for every row. The table shows the times on 2,000,000 rows.
| Mask | Time | Replacement | Time |
|---|---|---|---|
ts.index.time == time(12) |
241 ms | at_time |
25 ms |
| the two hour masks | 51 ms | between_time |
28 ms |
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 safe fix writes ts.at_time(time(12)). The unsafe fix writes ts.between_time("8:00", "16:00", inclusive="left").
The at_time fix applies to a time object, such as time(12). The rule leaves a comparison with text alone, because text never equals a time. The between_time fix applies to whole hours written as numbers. It is unsafe when the first hour is later than the second. Then the masks select nothing, and between_time selects the hours across midnight.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
index.time builds a Python time object for every row. Write $X.at_time($T).Browse the other rules of the pandas pack.