Skip to content

pandas/object-dates

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

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 check --fix --unsafe-fixes rewrites .dt.date (outside comparison operators) to .dt.normalize(). It is unsafe because .dt.normalize() returns datetime64 timestamps at midnight rather than Python date objects; .dt.time remains manual.

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

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.

See all rules in the pandas pack.