Skip to content

Commit 5940f15

Browse files
authored
BUG: pd.to_datetime([np.str_ objects]) GH#32264 (#45280)
1 parent 9ec76de commit 5940f15

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Categorical
118118

119119
Datetimelike
120120
^^^^^^^^^^^^
121-
-
121+
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
122122
-
123123

124124
Timedelta

pandas/_libs/tslib.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ cpdef array_to_datetime(
506506
elif isinstance(val, str):
507507
# string
508508
seen_string = True
509+
if type(val) is not str:
510+
# GH#32264 np.str_ object
511+
val = str(val)
509512

510513
if len(val) == 0 or val in nat_strings:
511514
iresult[i] = NPY_NAT
@@ -735,6 +738,10 @@ cdef _array_to_datetime_object(
735738
# GH 25978. No need to parse NaT-like or datetime-like vals
736739
oresult[i] = val
737740
elif isinstance(val, str):
741+
if type(val) is not str:
742+
# GH#32264 np.str_ objects
743+
val = str(val)
744+
738745
if len(val) == 0 or val in nat_strings:
739746
oresult[i] = 'NaT'
740747
continue

pandas/_libs/tslibs/parsing.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ cdef inline bint does_string_look_like_time(str parse_string):
239239

240240

241241
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'
242245
str date_string,
243246
bint dayfirst=False,
244247
bint yearfirst=False,
@@ -254,7 +257,7 @@ def parse_datetime_string(
254257
"""
255258

256259
cdef:
257-
object dt
260+
datetime dt
258261

259262
if not _does_string_look_like_datetime(date_string):
260263
raise ValueError('Given date string not likely a datetime.')

pandas/tests/tools/test_to_datetime.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,25 @@ def test_to_datetime_parse_timezone_keeps_name(self):
455455

456456

457457
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+
458477
@pytest.mark.parametrize(
459478
"s, _format, dt",
460479
[

0 commit comments

Comments
 (0)