Skip to content

behavior of NDBuffer.as_scalar #3026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
d-v-b opened this issue Apr 30, 2025 · 2 comments
Closed

behavior of NDBuffer.as_scalar #3026

d-v-b opened this issue Apr 30, 2025 · 2 comments

Comments

@d-v-b
Copy link
Contributor

d-v-b commented Apr 30, 2025

@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.

@d-v-b d-v-b mentioned this issue Apr 30, 2025
6 tasks
@d-v-b
Copy link
Contributor Author

d-v-b commented Apr 30, 2025

update: array.item() returns a native python value, but we want a numpy scalar, which we can get instead via array[()]. I implemented this over in #3027

@brokkoli71
Copy link
Member

I don't remember exactly, but there were some typing/mypy problems when just returning self.as_numpy_array().item().
But returning array[()] seems like the way to go anyway, thanks for fixing this.

@d-v-b d-v-b closed this as completed May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants