Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Bug Fixes
- Bug in ``Series`` arithmetic raises ``TypeError`` if it contains datetime-like as ``object`` dtype (:issue:`13043`)


- Bug in ``pd.to_datetime`` when passing ``bools``; will now respect the ``errors`` value (:issue:`13176`)

- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,29 @@ def test_to_datetime_tz_psycopg2(self):
dtype='datetime64[ns, UTC]')
tm.assert_index_equal(result, expected)

def test_datetime_bool(self):
# GH13176
with self.assertRaises(TypeError):
to_datetime(False)
self.assertTrue(to_datetime(False, errors="coerce") is tslib.NaT)
self.assertEqual(to_datetime(False, errors="ignore"), False)
with self.assertRaises(TypeError):
to_datetime(True)
self.assertTrue(to_datetime(True, errors="coerce") is tslib.NaT)
self.assertEqual(to_datetime(True, errors="ignore"), True)
with self.assertRaises(TypeError):
to_datetime([False, datetime.today()])
with self.assertRaises(TypeError):
to_datetime(['20130101', True])
tm.assert_index_equal(to_datetime([0, False, tslib.NaT, 0.0],
errors="coerce"),
DatetimeIndex([to_datetime(0), tslib.NaT,
tslib.NaT, to_datetime(0)]))
with self.assertRaises(TypeError):
pd.to_datetime(bool)
with self.assertRaises(TypeError):
pd.to_datetime(pd.to_datetime)

def test_unit(self):
# GH 11758
# test proper behavior with erros
Expand Down
6 changes: 6 additions & 0 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,12 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
iresult[i] = cast_from_unit(val, 'ns')
except:
iresult[i] = NPY_NAT
elif not util.is_string_object(val):
if is_coerce:
iresult[i] = NPY_NAT
else:
raise TypeError("{0} is not convertible to datetime"
.format(type(val)))
else:
try:
if len(val) == 0 or val in _nat_strings:
Expand Down