File tree Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -118,7 +118,7 @@ Categorical
118
118
119
119
Datetimelike
120
120
^^^^^^^^^^^^
121
- -
121
+ - Bug in :func: ` to_datetime ` with sequences of `` np.str_ `` objects incorrectly raising ( :issue: ` 32264 `)
122
122
-
123
123
124
124
Timedelta
Original file line number Diff line number Diff line change @@ -506,6 +506,9 @@ cpdef array_to_datetime(
506
506
elif isinstance (val, str ):
507
507
# string
508
508
seen_string = True
509
+ if type (val) is not str :
510
+ # GH#32264 np.str_ object
511
+ val = str (val)
509
512
510
513
if len (val) == 0 or val in nat_strings:
511
514
iresult[i] = NPY_NAT
@@ -735,6 +738,10 @@ cdef _array_to_datetime_object(
735
738
# GH 25978. No need to parse NaT-like or datetime-like vals
736
739
oresult[i] = val
737
740
elif isinstance (val, str ):
741
+ if type (val) is not str :
742
+ # GH#32264 np.str_ objects
743
+ val = str (val)
744
+
738
745
if len (val) == 0 or val in nat_strings:
739
746
oresult[i] = ' NaT'
740
747
continue
Original file line number Diff line number Diff line change @@ -239,6 +239,9 @@ cdef inline bint does_string_look_like_time(str parse_string):
239
239
240
240
241
241
def parse_datetime_string (
242
+ # NB: This will break with np.str_ (GH#32264) even though
243
+ # isinstance(npstrobj , str ) evaluates to True , so caller must ensure
244
+ # the argument is *exactly* 'str'
242
245
str date_string ,
243
246
bint dayfirst = False ,
244
247
bint yearfirst = False ,
@@ -254,7 +257,7 @@ def parse_datetime_string(
254
257
"""
255
258
256
259
cdef:
257
- object dt
260
+ datetime dt
258
261
259
262
if not _does_string_look_like_datetime(date_string ):
260
263
raise ValueError (' Given date string not likely a datetime.' )
Original file line number Diff line number Diff line change @@ -455,6 +455,25 @@ def test_to_datetime_parse_timezone_keeps_name(self):
455
455
456
456
457
457
class TestToDatetime :
458
+ def test_to_datetime_np_str (self ):
459
+ # GH#32264
460
+ value = np .str_ ("2019-02-04 10:18:46.297000+0000" )
461
+
462
+ ser = Series ([value ])
463
+
464
+ exp = Timestamp ("2019-02-04 10:18:46.297000" , tz = "UTC" )
465
+
466
+ assert to_datetime (value ) == exp
467
+ assert to_datetime (ser .iloc [0 ]) == exp
468
+
469
+ res = to_datetime ([value ])
470
+ expected = Index ([exp ])
471
+ tm .assert_index_equal (res , expected )
472
+
473
+ res = to_datetime (ser )
474
+ expected = Series (expected )
475
+ tm .assert_series_equal (res , expected )
476
+
458
477
@pytest .mark .parametrize (
459
478
"s, _format, dt" ,
460
479
[
You can’t perform that action at this time.
0 commit comments