{"record":{"id":"ec18464f3e38a607","repo":"pandas-dev/pandas","slug":"pd-api-extensions-take-requires-a-numpy-ndarray-e","errorCode":null,"errorMessage":"pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, Index, Series, or NumpyExtensionArray got {type(arr).__name__}.","messagePattern":"pd\\.api\\.extensions\\.take requires a numpy\\.ndarray, ExtensionArray, Index, Series, or NumpyExtensionArray got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":1374,"sourceCode":"    >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1])\n    array([10, 10, 30])\n\n    Setting ``allow_fill=True`` will place `fill_value` in those positions.\n\n    >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True)\n    array([10., 10., nan])\n\n    >>> pd.api.extensions.take(\n    ...     np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, fill_value=-10\n    ... )\n    array([ 10,  10, -10])\n    \"\"\"\n    if not isinstance(\n        arr,\n        (np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries, ABCNumpyExtensionArray),\n    ):\n        # GH#52981\n        raise TypeError(\n            \"pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, \"\n            f\"Index, Series, or NumpyExtensionArray got {type(arr).__name__}.\"\n        )\n\n    indices = ensure_platform_int(indices)\n\n    if allow_fill:\n        # Pandas style, -1 means NA\n        validate_indices(indices, arr.shape[axis])\n        # error: Argument 1 to \"take_nd\" has incompatible type\n        # \"ndarray[Any, Any] | ExtensionArray | Index | Series\"; expected\n        # \"ndarray[Any, Any]\"\n        result = take_nd(\n            arr,  # type: ignore[arg-type]\n            indices,\n            axis=axis,\n            allow_fill=True,\n            fill_value=fill_value,","sourceCodeStart":1356,"sourceCodeEnd":1392,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/algorithms.py#L1356-L1392","documentation":"Raised by pd.api.extensions.take when the source array is not one of the supported types (numpy.ndarray, ExtensionArray, Index, Series, NumpyExtensionArray). The take routine needs array-like indexing semantics; a plain Python list, dict, or scalar cannot be safely indexed.","triggerScenarios":"pd.api.extensions.take([10, 20, 30], [0, 1]) with a Python list; passing a set or tuple; passing a scalar value where an array is required.","commonSituations":"Using pd.api.extensions.take as a generic indexing helper with native lists; refactors that drop the np.asarray conversion before calling take.","solutions":["Convert the input to np.ndarray or a pandas Series/Index first.","Use np.take for plain lists, or pd.api.extensions.take(np.asarray(lst), idx).","Validate the type before calling take."],"exampleFix":"# before\npd.api.extensions.take([10, 20, 30], [0, 2])\n# after\npd.api.extensions.take(np.array([10, 20, 30]), [0, 2])","handlingStrategy":"type-guard","validationCode":"import numpy as np, pandas as pd\n\ndef safe_take(arr, indices, **kw):\n    if not isinstance(arr, (np.ndarray, pd.Index, pd.Series, pd.api.extensions.ExtensionArray)):\n        arr = np.asarray(arr)\n    return pd.api.extensions.take(arr, indices, **kw)","typeGuard":"import numpy as np, pandas as pd\n\ndef is_takeable(arr) -> bool:\n    return isinstance(arr, (np.ndarray, pd.Index, pd.Series, pd.api.extensions.ExtensionArray))","tryCatchPattern":null,"preventionTips":["Convert lists to np.ndarray before pd.api.extensions.take.","Use np.take for plain lists when code remapping isn't needed.","Validate the source type at the boundary."],"tags":["take","arraylike","validation","extensions"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}