Skip to content

Commit 8b92517

Browse files
undo changes related to datetimelike input validation
1 parent aa89c32 commit 8b92517

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@
130130
from pandas.core.arrays.integer import IntegerArray
131131
import pandas.core.common as com
132132
from pandas.core.construction import (
133+
array as pd_array,
133134
ensure_wrapped_if_datetimelike,
134135
extract_array,
135-
sanitize_array,
136136
)
137137
from pandas.core.indexers import (
138138
check_array_indexer,
@@ -667,10 +667,12 @@ def _validate_listlike(self, value, allow_object: bool = False):
667667
msg = self._validation_error_message(value, True)
668668
raise TypeError(msg) from err
669669

670-
# Do type inference if necessary up front
670+
# Do type inference if necessary up front (after unpacking
671+
# NumpyExtensionArray)
671672
# e.g. we passed PeriodIndex.values and got an ndarray of Periods
672-
value = sanitize_array(value, index=None, allow_2d=True)
673-
value = ensure_wrapped_if_datetimelike(value)
673+
value = extract_array(value, extract_numpy=True)
674+
value = pd_array(value)
675+
value = extract_array(value, extract_numpy=True)
674676

675677
if is_all_strings(value):
676678
# We got a StringArray

pandas/core/construction.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pandas.core.dtypes.common import (
3838
ensure_object,
3939
is_list_like,
40+
is_object_dtype,
4041
pandas_dtype,
4142
)
4243
from pandas.core.dtypes.dtypes import NumpyEADtype
@@ -48,6 +49,8 @@
4849
)
4950
from pandas.core.dtypes.missing import isna
5051

52+
import pandas.core.common as com
53+
5154
if TYPE_CHECKING:
5255
from collections.abc import Sequence
5356

@@ -703,12 +706,23 @@ def _sanitize_ndim(
703706
result = _maybe_repeat(result, index)
704707

705708
elif result.ndim > 1:
706-
if allow_2d:
707-
return result
708-
raise ValueError(
709-
f"Data must be 1-dimensional, got ndarray of shape {data.shape} instead"
710-
)
709+
if isinstance(data, np.ndarray):
710+
if allow_2d:
711+
return result
712+
raise ValueError(
713+
f"Data must be 1-dimensional, got ndarray of shape {data.shape} instead"
714+
)
715+
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
716+
# i.e. NumpyEADtype("O")
711717

718+
result = com.asarray_tuplesafe(data, dtype=np.dtype("object"))
719+
cls = dtype.construct_array_type()
720+
result = cls._from_sequence(result, dtype=dtype)
721+
else:
722+
# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
723+
# "Union[dtype[Any], ExtensionDtype, None]"; expected "Union[str,
724+
# dtype[Any], None]"
725+
result = com.asarray_tuplesafe(data, dtype=dtype) # type: ignore[arg-type]
712726
return result
713727

714728

pandas/tests/arrays/test_datetimelike.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ def test_searchsorted(self):
297297
assert result == 10
298298

299299
@pytest.mark.parametrize("box", [None, "index", "series"])
300-
def test_searchsorted_castable_strings(
301-
self, arr1d, box, string_storage, using_infer_string
302-
):
300+
def test_searchsorted_castable_strings(self, arr1d, box, string_storage):
303301
arr = arr1d
304302
if box is None:
305303
pass
@@ -335,8 +333,7 @@ def test_searchsorted_castable_strings(
335333
TypeError,
336334
match=re.escape(
337335
f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', "
338-
"or array of those. Got "
339-
f"{'str' if using_infer_string else 'object'} array instead."
336+
"or array of those. Got string array instead."
340337
),
341338
):
342339
arr.searchsorted([str(arr[1]), "baz"])

0 commit comments

Comments
 (0)