Skip to content

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.date
days = df["time"].dt.normalize()

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.

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().

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.