pandas/object-dates
| Rule | object-dates |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
dt.date and dt.time turn a datetime column into a column of Python objects, which is slow and loses the datetime methods. Use dt.normalize() or dt.floor("D") to keep datetime64.
days = df["time"].dt.datedays = df["time"].dt.normalize()What the rule reports
Section titled “What the rule reports”The rule reports df["d"].dt.date and dt.time, which turn a datetime64 column into a column of Python objects. The rule leaves alone a dt.date that is one side of a comparison, because the replacement dt.normalize() keeps datetime64. A datetime64 value compared with a Python date raises TypeError.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
normalize returns timestamps at midnight and not Python dates, so keep dt.date when another library needs Python dates. For dt.time, normalize drops the time of day, so keep the time as the time since midnight, df["time"] - df["time"].dt.normalize().
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
dt.$P makes a column of Python objects. Use dt.normalize() or dt.floor("D") to keep datetime64, and compare the result with a pd.Timestamp, not a date.Browse the other rules of the pandas pack.