Skip to content

Use errors='coerce' when converting to numerical or datetime in TableVectorizer's _apply_cast - #666

Merged
GaelVaroquaux merged 9 commits into
skrub-data:mainfrom
LeoGrin:fix_changing_dtypes
Jul 21, 2023
Merged

GaelVaroquaux merged 9 commits into
skrub-data:mainfrom
LeoGrin:fix_changing_dtypes

Conversation

@LeoGrin

@LeoGrin LeoGrin commented Jul 20, 2023 •

Copy link
Copy Markdown
Contributor

Fix #631

Questions:

  • do we want to warn the user if there is an issue with one of the inferred types?
  • what we would like to do with the new entry of the wrong type would be to ignore it. If we convert it to nan, some model may want to use what it learnt about missing values during training on this entry, though it wouldn't actually be missing.

@GaelVaroquaux

Copy link
Copy Markdown
Member

Answers to your question:

  • I think that we should warn, possibly with a dedicated error to make it easy to catch this error
  • We should attempt the "best conversion possible", whatever that means. This may not be easy.

I'll review tomorrow, it's getting too late

@GaelVaroquaux GaelVaroquaux 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.

One comment: I think that we should code a mechanism to raise a warning. We can discuss this to gauge how important it is.

Comment thread skrub/_table_vectorizer.py Outdated
# we want to ignore entries that cannot be converted
# to this dtype
if pd.api.types.is_numeric_dtype(dtype):
X[col] = pd.to_numeric(X[col], errors="coerce")

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.

With the current usage of "coerce" it is hard to do a warning if an invalid value is encountered.

If we feel that it is important to warn (I am leaning in this direction but not 100% sure), one way to do it is to do a try/except with 'errors="raise"', if an error (the "except" block), capture it, pop up a warning (ideally informative) and then do the same call but with 'error="coerce"'.

@LeoGrin
LeoGrin requested a review from GaelVaroquaux July 21, 2023 13:25
elif pd.api.types.is_datetime64_any_dtype(dtype):
X[col] = pd.to_datetime(X[col], errors="coerce")
else:
# this should not happen

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.

❤️

@GaelVaroquaux
GaelVaroquaux merged commit f9f2b1c into skrub-data:main Jul 21, 2023
@GaelVaroquaux

Copy link
Copy Markdown
Member

Thanks!! Merged

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TableVectorizer fails when the type change between train and test

2 participants