Skip to content

API: construction from custom object with __array__ method #53085

Open
@jbrockmendel

Description

@jbrockmendel
# based on test_constructor_ndarray_like

class ArrayLike:
    def __init__(self, array) -> None:
        self.array = array

    # __array__ but no __iter__, so is_list_like(obj) is False
    def __array__(self, dtype=None) -> np.ndarray:
        return self.array


obj = ArrayLike(np.arange(5))
result = pd.Index(obj)    # <- unpacks array
result2 = pd.Series(obj)  # <- treats obj as a scalar
result3 = pd.array(obj)   # <- raises "TypeError: 'ArrayLike' object is not iterable"

We should aim for consistent behavior between these three constructors. I lean towards preferring the raising behavior here, but could be convinced otherwise as long as we move towards consistency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorConstructorsSeries/DataFrame/Index/pd.array Constructors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions