Skip to content

fix(ToDatetime): accept pandas columns of datetime.date objects - #2231

Open
Sanjays2402 wants to merge 2 commits into
skrub-data:mainfrom
Sanjays2402:fix/to-datetime-date-objects
Open

fix(ToDatetime): accept pandas columns of datetime.date objects#2231
Sanjays2402 wants to merge 2 commits into
skrub-data:mainfrom
Sanjays2402:fix/to-datetime-date-objects

Conversation

@Sanjays2402

Copy link
Copy Markdown

Description

Fixes: #2084

Pandas has no dedicated dtype for datetime.date, so a column built from date objects has the object dtype: it is neither a date column nor a string column, so ToDatetime rejected it and the DatetimeEncoder/TableVectorizer never saw it. The equivalent polars column has the Date dtype and was handled correctly, so the two backends behaved differently.

Changes

  • Cast pandas object columns that contain only datetime.date to Datetime before the dtype checks in ToDatetime.fit_transform. Columns that are not entirely made of dates, and columns that fail to convert, are returned unchanged, so string parsing and the existing rejections are unaffected.
  • Add a non-regression test, and update test_to_datetime_func, which previously excluded the pandas date-col because of this bug.
  • Changelog entry.

Testing

  • Unit tests added/updated
  • Integration tests passed
  • Manual checks performed: new test fails on main with RejectColumn and passes with the fix (stash-verified); skrub/tests/test_to_datetime.py and test_datetime_encoder.py pass

Checklist

  • Code follows project style guidelines (linting passes).
  • Tests added/updated for changes.
  • All tests pass locally.
  • Documentation updated (if needed).

Pandas has no dedicated dtype for datetime.date, so a column built from
date objects has the object dtype: it is neither a date column nor a
string column, so ToDatetime rejected it and the DatetimeEncoder never
saw it. The equivalent polars column has the Date dtype and was handled
correctly, making the behaviour inconsistent between the two backends.

Cast object columns that contain only datetime.date to Datetime before
the dtype checks in fit_transform. Columns that are not entirely made of
dates, and columns that fail to convert, are left untouched so string
parsing and the existing rejections are unaffected.

Adds a non-regression test and updates test_to_datetime_func, which
previously excluded the pandas date column because of this bug.

@rcap107 rcap107 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sanjays2402, thanks for this PR.

It looks good so far, but coverage of the tests needs to be improved. Since the missing lines are in the _cast_date_objects method, you can add a test specifically for that function, rather than using ToDatetime.

Other than that, I think this issue had some kind of hidden caveat that I currently do not remember, and I'd like to weigh in with some other maintainers that are currently away before merging.

Comment thread skrub/_to_datetime.py
return column
try:
return pd.to_datetime(column)
except Exception:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this exception to the specific exception that is raised by pd.to_datetime instead

Comment thread skrub/_to_datetime.py
if not (sbd.is_pandas(column) and sbd.is_object(column)):
return column
not_null = sbd.drop_nulls(column)
if sbd.shape(not_null)[0] == 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line isn't covered by tests and coverage is complaining about it

Comment thread skrub/_to_datetime.py
try:
return pd.to_datetime(column)
except Exception:
return column

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this line, it should be covered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG - Pandas columns created with datetime.date are not recognized as "date"

2 participants