Skip to content

behavior of NDBuffer.as_scalar #3026

Closed
Closed
@d-v-b

Description

@d-v-b

@brokkoli71 could you explain the logic of NDBuffer.as_scalar?

I noticed that it's doing some data type-specific stuff (there's a special path for np.datetime64). Why was this necessary?

I'm asking because this function is erroring over in #2874 when I add timedelta data types, because timedeltas are not caught in the datetime special case. But I find the need for dtype-specific special cases here suspicious.

For reference, if I replace that function with something much simpler:

    def as_scalar(self) -> ScalarType:
        """Returns the buffer as a scalar value"""
        if self._data.size != 1:
            raise ValueError("Buffer does not contain a single scalar value")
        return self.as_numpy_array().item()

I get errors in a different place, but it looks like those errors are revealing real indexing bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions