-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Correct bool to datetime conversion (behaviour based on int/float processing) #13176
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
Conversation
@@ -2230,7 +2230,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', | |||
seen_integer=1 | |||
else: | |||
try: | |||
if len(val) == 0 or val in _nat_strings: | |||
if not hasattr(val, "__len__") or len(val) == 0 or val in _nat_strings: |
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.
instead of this make a new path for bool detection
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.
modified
@@ -2214,7 +2214,8 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', | |||
iresult[i] = NPY_NAT | |||
continue | |||
raise | |||
|
|||
elif is_bool_object(val): |
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 is only true for coerce, pls follow the existing patterns
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.
changed
Current coverage is 84.12%@@ master #13176 diff @@
==========================================
Files 138 138
Lines 50587 50385 -202
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
- Hits 42593 42383 -210
- Misses 7994 8002 +8
Partials 0 0
|
self.assertTrue(to_datetime(True, errors="coerce") is tslib.NaT) | ||
self.assertEqual(to_datetime(True, errors="raise"), to_datetime(1)) | ||
self.assertEqual(to_datetime(True, errors="ignore"), to_datetime(1)) | ||
self.assert_numpy_array_equal(to_datetime([True, False, tslib.NaT]), |
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.
use tm.assert_index_equal
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.
changed
add a whatsnew entry |
@@ -608,6 +608,18 @@ def test_parsers(self): | |||
self.assertTrue(result1 is tslib.NaT) | |||
self.assertTrue(result1 is tslib.NaT) | |||
|
|||
def test_datetime_bool(self): | |||
self.assertEqual(to_datetime(False), to_datetime(0)) |
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.
add the issue number as a comment
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.
added
added whatsnew |
build is green |
@@ -608,6 +608,19 @@ def test_parsers(self): | |||
self.assertTrue(result1 is tslib.NaT) | |||
self.assertTrue(result1 is tslib.NaT) | |||
|
|||
def test_datetime_bool(self): | |||
# GH13176 | |||
self.assertEqual(to_datetime(False), to_datetime(0)) |
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.
can you also add some tests where bools are intermixed with valid (and also invalid conversions)
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.
added
Would you like to see any other changes? Thanks |
1 similar comment
Would you like to see any other changes? Thanks |
@gliptak actually I think I steered you wrong here. These should raise if They should not convert to 0/1 datetimes. |
What is the informative message when |
so a messge like |
@jreback Is this the expected processing? |
if val == NPY_NAT: | ||
iresult[i] = NPY_NAT | ||
elif is_raise: | ||
raise ValueError("boolean is not convertible to 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.
follow the existing pattern for 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.
you need to raise TypeError
so it kicks out to the object loop
add a test that has something like
pd.to_datetime(['20130101',True])
ok we have another case. I accidently gave it this (from the original issue)
|
updated |
build is green |
thanks @gliptak |
git diff upstream/master | flake8 --diff