Skip to content

Commit f2ce0ac

Browse files
gliptakjreback
authored andcommitted
ERR: error in datetime conversion with non-convertibles
closes #11853 Author: Gábor Lipták <[email protected]> Closes #13176 from gliptak/dtbool1 and squashes the following commits: 5179d1d [Gábor Lipták] Bug in pd.to_datetime when passing bools; will now respect the errors value
1 parent b4e2d34 commit f2ce0ac

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Bug Fixes
245245
- Bug in ``Series`` arithmetic raises ``TypeError`` if it contains datetime-like as ``object`` dtype (:issue:`13043`)
246246

247247

248+
- Bug in ``pd.to_datetime()`` when passing invalid datatypes (e.g. bool); will now respect the ``errors`` keyword (:issue:`13176`)
248249

249250
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
250251
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)

pandas/tseries/tests/test_timeseries.py

+27
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,33 @@ def test_to_datetime_tz_psycopg2(self):
22922292
dtype='datetime64[ns, UTC]')
22932293
tm.assert_index_equal(result, expected)
22942294

2295+
def test_datetime_bool(self):
2296+
# GH13176
2297+
with self.assertRaises(TypeError):
2298+
to_datetime(False)
2299+
self.assertTrue(to_datetime(False, errors="coerce") is tslib.NaT)
2300+
self.assertEqual(to_datetime(False, errors="ignore"), False)
2301+
with self.assertRaises(TypeError):
2302+
to_datetime(True)
2303+
self.assertTrue(to_datetime(True, errors="coerce") is tslib.NaT)
2304+
self.assertEqual(to_datetime(True, errors="ignore"), True)
2305+
with self.assertRaises(TypeError):
2306+
to_datetime([False, datetime.today()])
2307+
with self.assertRaises(TypeError):
2308+
to_datetime(['20130101', True])
2309+
tm.assert_index_equal(to_datetime([0, False, tslib.NaT, 0.0],
2310+
errors="coerce"),
2311+
DatetimeIndex([to_datetime(0), tslib.NaT,
2312+
tslib.NaT, to_datetime(0)]))
2313+
2314+
def test_datetime_invalid_datatype(self):
2315+
# GH13176
2316+
2317+
with self.assertRaises(TypeError):
2318+
pd.to_datetime(bool)
2319+
with self.assertRaises(TypeError):
2320+
pd.to_datetime(pd.to_datetime)
2321+
22952322
def test_unit(self):
22962323
# GH 11758
22972324
# test proper behavior with erros

pandas/tslib.pyx

+15-2
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,10 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
22202220
iresult = result.view('i8')
22212221
for i in range(n):
22222222
val = values[i]
2223+
22232224
if _checknull_with_nat(val):
22242225
iresult[i] = NPY_NAT
2226+
22252227
elif PyDateTime_Check(val):
22262228
seen_datetime=1
22272229
if val.tzinfo is not None:
@@ -2250,6 +2252,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
22502252
iresult[i] = NPY_NAT
22512253
continue
22522254
raise
2255+
22532256
elif PyDate_Check(val):
22542257
iresult[i] = _date_to_datetime64(val, &dts)
22552258
try:
@@ -2260,6 +2263,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
22602263
iresult[i] = NPY_NAT
22612264
continue
22622265
raise
2266+
22632267
elif util.is_datetime64_object(val):
22642268
if get_datetime64_value(val) == NPY_NAT:
22652269
iresult[i] = NPY_NAT
@@ -2273,8 +2277,8 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
22732277
continue
22742278
raise
22752279

2276-
# these must be ns unit by-definition
22772280
elif is_integer_object(val) or is_float_object(val):
2281+
# these must be ns unit by-definition
22782282

22792283
if val != val or val == NPY_NAT:
22802284
iresult[i] = NPY_NAT
@@ -2292,7 +2296,10 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
22922296
iresult[i] = cast_from_unit(val, 'ns')
22932297
except:
22942298
iresult[i] = NPY_NAT
2295-
else:
2299+
2300+
elif util.is_string_object(val):
2301+
# string
2302+
22962303
try:
22972304
if len(val) == 0 or val in _nat_strings:
22982305
iresult[i] = NPY_NAT
@@ -2340,6 +2347,12 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
23402347
iresult[i] = NPY_NAT
23412348
continue
23422349
raise
2350+
else:
2351+
if is_coerce:
2352+
iresult[i] = NPY_NAT
2353+
else:
2354+
raise TypeError("{0} is not convertible to datetime"
2355+
.format(type(val)))
23432356

23442357
if seen_datetime and seen_integer:
23452358
# we have mixed datetimes & integers

0 commit comments

Comments
 (0)