Skip to content

BUG: pd.to_datetime([np.str_ objects]) GH#32264 #45280

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

Merged
merged 1 commit into from
Jan 10, 2022
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Categorical

Datetimelike
^^^^^^^^^^^^
-
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
-

Timedelta
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ cpdef array_to_datetime(
elif isinstance(val, str):
# string
seen_string = True
if type(val) is not str:
# GH#32264 np.str_ object
val = str(val)

if len(val) == 0 or val in nat_strings:
iresult[i] = NPY_NAT
Expand Down Expand Up @@ -735,6 +738,10 @@ cdef _array_to_datetime_object(
# GH 25978. No need to parse NaT-like or datetime-like vals
oresult[i] = val
elif isinstance(val, str):
if type(val) is not str:
# GH#32264 np.str_ objects
val = str(val)

if len(val) == 0 or val in nat_strings:
oresult[i] = 'NaT'
continue
Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ cdef inline bint does_string_look_like_time(str parse_string):


def parse_datetime_string(
# NB: This will break with np.str_ (GH#32264) even though
# isinstance(npstrobj, str) evaluates to True, so caller must ensure
# the argument is *exactly* 'str'
str date_string,
bint dayfirst=False,
bint yearfirst=False,
Expand All @@ -254,7 +257,7 @@ def parse_datetime_string(
"""

cdef:
object dt
datetime dt

if not _does_string_look_like_datetime(date_string):
raise ValueError('Given date string not likely a datetime.')
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,25 @@ def test_to_datetime_parse_timezone_keeps_name(self):


class TestToDatetime:
def test_to_datetime_np_str(self):
# GH#32264
value = np.str_("2019-02-04 10:18:46.297000+0000")

ser = Series([value])

exp = Timestamp("2019-02-04 10:18:46.297000", tz="UTC")

assert to_datetime(value) == exp
assert to_datetime(ser.iloc[0]) == exp

res = to_datetime([value])
expected = Index([exp])
tm.assert_index_equal(res, expected)

res = to_datetime(ser)
expected = Series(expected)
tm.assert_series_equal(res, expected)

@pytest.mark.parametrize(
"s, _format, dt",
[
Expand Down