Skip to content

Commit 32474d9

Browse files
authored
BUG: to_datetime(..., infer_datetime_format=True) fails with np.str_input (#48970)
* BUG: to_datetime(..., infer_datetime_format=True) fails with np.str_ input * add return Co-authored-by: MarcoGorelli <>
1 parent 814fd82 commit 32474d9

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

pandas/_libs/tslibs/parsing.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,6 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
963963
datetime format string (for `strftime` or `strptime`),
964964
or None if it can't be guessed.
965965
"""
966-
967-
if not isinstance(dt_str, str):
968-
return None
969-
970966
day_attribute_and_format = (('day',), '%d', 2)
971967

972968
# attr name, format, padding (if any)

pandas/core/tools/datetimes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
125125
# ---------------------------------------------------------------------
126126

127127

128-
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False):
129-
# Try to guess the format based on the first non-NaN element
128+
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str | None:
129+
# Try to guess the format based on the first non-NaN element, return None if can't
130130
non_nan_elements = notna(arr).nonzero()[0]
131131
if len(non_nan_elements):
132-
return guess_datetime_format(arr[non_nan_elements[0]], dayfirst=dayfirst)
132+
if type(first_non_nan_element := arr[non_nan_elements[0]]) is str:
133+
# GH#32264 np.str_ object
134+
return guess_datetime_format(first_non_nan_element, dayfirst=dayfirst)
135+
return None
133136

134137

135138
def should_cache(

pandas/tests/tools/test_to_datetime.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ def test_to_datetime_mixed_datetime_and_string(self):
468468
expected = to_datetime([d1, d2]).tz_convert(pytz.FixedOffset(-60))
469469
tm.assert_index_equal(res, expected)
470470

471-
def test_to_datetime_np_str(self):
471+
@pytest.mark.parametrize("infer_datetime_format", [True, False])
472+
def test_to_datetime_np_str(self, infer_datetime_format):
472473
# GH#32264
474+
# GH#48969
473475
value = np.str_("2019-02-04 10:18:46.297000+0000")
474476

475477
ser = Series([value])
@@ -479,11 +481,11 @@ def test_to_datetime_np_str(self):
479481
assert to_datetime(value) == exp
480482
assert to_datetime(ser.iloc[0]) == exp
481483

482-
res = to_datetime([value])
484+
res = to_datetime([value], infer_datetime_format=infer_datetime_format)
483485
expected = Index([exp])
484486
tm.assert_index_equal(res, expected)
485487

486-
res = to_datetime(ser)
488+
res = to_datetime(ser, infer_datetime_format=infer_datetime_format)
487489
expected = Series(expected)
488490
tm.assert_series_equal(res, expected)
489491

0 commit comments

Comments
 (0)