-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: Parse dates with empty space (#6428) #14862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -947,6 +947,18 @@ def test_to_datetime_on_datetime64_series(self): | |
result = to_datetime(s) | ||
self.assertEqual(result[0], s[0]) | ||
|
||
def test_to_datetime_with_space_in_series(self): | ||
# GH 6428 | ||
s = Series(['10/18/2006', '10/18/2008', ' ']) | ||
tm.assertRaises(ValueError, lambda: to_datetime(s, errors='raise')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is right |
||
result_coerce = to_datetime(s, errors='coerce') | ||
expected_coerce = Series([datetime(2006, 10, 18), | ||
datetime(2008, 10, 18), | ||
pd.NaT]) | ||
tm.assert_series_equal(result_coerce, expected_coerce) | ||
result_ignore = to_datetime(s, errors='ignore') | ||
tm.assert_series_equal(result_ignore, s) | ||
|
||
def test_to_datetime_with_apply(self): | ||
# this is only locale tested with US/None locales | ||
_skip_if_has_locale() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should actually parse as datetime and make the space a NaT. So this 'works' in that it doesn't convert as today's date, but it is not registering as missing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it looks like
errors='ignore'
is set when parsing datetimes withread_csv
(example). I can change this toerrors='coerce'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, I think we did that on purpose..
that whole clause is bogus,
errors='ignore'
means it will never raise.but its a bit tricky, you want to try to parse, then fallback on a softer-parse if necessary.
try playing around with this and see what happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah thanks for the insight. I'll tinker with it later tonight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were correct; current tests require
errors='ignore'
.This is tricky since
errors='ignore'
anderrors='coerce'
both terminate the parse withNaT
s or returning the input. Would it be possible to introduce a new kwarg (e.g.date_errors
) to pass ignore/coerce/raise, intoto_datetime
withinread_csv
? It could default to ignore.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would make the API even more complicated. Generally supporting non-standard datetime parsing should simply use
pd.to_datetime
post-read_csv.@jorisvandenbossche any thoughts here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point.
I could clarify in the docs that if
parse_dates
cannot parse a date the column is returned untouched (which seems like the current behavior) and the user can coerce a space or any unparseable date toNaT
post-read_csv
withto_datetime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that's prob the best soln here (pls add to doc-string & a small warning/note section in io.rst if you don't mind).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, I agree that we should not add kwargs to read_csv for something like this. If you want the date parser, your dates should just be able to be parsed with the default settings, otherwise you can just use
to_datetime
. PR for doc clarification certainly welcome!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which you already included here :-)