Code Monkey home page Code Monkey logo

Comments (3)

gunnarvoet avatar gunnarvoet commented on June 11, 2024 1

Thanks so much for explaining this in detail, @Ockenfuss and my apologies for taking up your time! I thought the expression would evaluate from left to right. Using np.logical_not() is a great suggestion.

from xarray.

welcome avatar welcome commented on June 11, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

from xarray.

Ockenfuss avatar Ockenfuss commented on June 11, 2024

This is actually expected behaviour in python. If you do ~test.sum(dim="x"), the call to sum() has a higher precedence than the bitwise invert operator, represented by ~. Therefore, test.sum(dim='x') evaluates to 2 and then ~2 evaluates to -3. The latter is due to the way integers are represented as bits in python.

In general, it might be better to use np.logical_not(test) in order to invert the array. logical_not also works in case your data consists of integers instead of booleans:

import numpy as np
import xarray as xr

data_int=np.array([1, 0, 1, 0])
test_int=xr.DataArray(data_int, coords=dict(x=np.arange(4)))
assert np.logical_not(test_int).sum(dim='x')==2 #works
assert (~test_int).sum(dim='x')==2 #fails

from xarray.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.