{"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":"exception","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/algorithms.py#L1356-L1392","documentation":"pd.api.extensions.take() is a public utility for pandas-style array indexing (supporting negative indices and fill-value semantics). It only accepts numpy.ndarray, ExtensionArray, Index, Series, or NumpyExtensionArray as the source array. This guard (GH#52981) rejects Python lists, tuples, sets, and other non-array-like inputs early, before they reach the internal take_nd logic where they would fail with a less clear error.","triggerScenarios":"Calling pd.api.extensions.take([1, 2, 3], [0, 1]) with a plain Python list instead of an ndarray. Passing a tuple, set, or custom iterable as the first argument. Using a dictionary or scalar where an array is expected.","commonSituations":"Using pd.api.extensions.take as a safer alternative to numpy.take and forgetting to convert a list to an array. Passing data from JSON deserialization (which yields lists) directly without conversion. Interfacing with libraries that return Python lists rather than numpy arrays.","solutions":["Convert the input to np.asarray() first: pd.api.extensions.take(np.asarray(data), indices).","Wrap in pd.Series(): pd.api.extensions.take(pd.Series(data), indices).","Use the type the function expects by construction — build your data as np.ndarray from the start."],"exampleFix":"# before\npd.api.extensions.take([10, 20, 30], [0, 0, -1])\n\n# after\npd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1])","handlingStrategy":"validation","validationCode":"def safe_take(arr, indices, **kwargs):\n    if not isinstance(arr, (np.ndarray, pd.Series, pd.Index, pd.api.extensions.ExtensionArray)):\n        arr = np.asarray(arr)\n    return pd.api.extensions.take(arr, indices, **kwargs)","typeGuard":"def is_takeable(arr) -> bool:\n    return isinstance(\n        arr,\n        (np.ndarray, pd.Series, pd.Index, pd.api.extensions.ExtensionArray)\n    )","tryCatchPattern":"try:\n    result = pd.api.extensions.take(data, indices)\nexcept TypeError as e:\n    if \"requires a numpy.ndarray\" in str(e):\n        result = pd.api.extensions.take(np.asarray(data), indices)\n    else:\n        raise","preventionTips":["Convert lists and tuples to np.asarray() before calling pd.api.extensions.take.","Document the expected input type when wrapping pd.api.extensions.take in utility functions.","Use np.asarray() as a universal normalization step for array-expecting APIs."],"tags":["pandas","take","api-extensions","type-validation","typeerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}