0

I have a variable (my_var) whose values are defined by 3 coordinates. It looks something like the following:

my_var.sum
<bound method DataArray.sum of <xarray.DataArray (Time: 18, south_north: 270, west_east: 312)>
dask.array<mean_ag..., shape=(18, 270, 312), dtype=float32, chunksize=(1, 270, 312)>
Coordinates:
* south_north  (south_north) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
* west_east    (west_east) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
* Time         (Time) datetime64[ns] 2014-11-06T07:00:00 ...

How can I find the point where my_var takes its minimum value for a certain Time along with this value?

So far I've tried several versions of min, but I can't seem to make them work.

6
  • Can you give a MWE of the object in question? Commented Jan 4, 2017 at 9:57
  • I'm sorry, but what do you mean by that? I've googled it and it appears to mean multi-word expression. What does that imply? @innisfree Commented Jan 4, 2017 at 10:03
  • Minimum working example, i.e. in this case a snippet of code that makes an object of the same type, shape etc as your my_var Commented Jan 4, 2017 at 10:07
  • The object is read directly from a dataset, so I don't know about that Commented Jan 4, 2017 at 10:11
  • Not the same data, but same type and shape of object Commented Jan 4, 2017 at 10:17

1 Answer 1

1

You can use either numpy.min (keyword-argument axis) or min (keyword-argument key), depending on the layout of your data.

If your data is laid out as

data = [(attr1, attr2, ...), ...]

then use min(data, key=lambda p: p[0] to find the minimum with respect to attr1 for example.

If you have a multi-dimensional array representing your data then I suggest using numpy.min while specifying the desired axis.

Sign up to request clarification or add additional context in comments.

4 Comments

It already worked with the min function but would you mind explaining a little about the key keyword? Does it just find the minimum respect to an index? There's barely any documentation about it
Also, I can't seem to make it work using numpy.min. Is the axis keyword associated to the name of the axis in the dataset? I can't find documentation on this either, there's just documentation for minimum but not for min and they don't seem to be the same function.
key is a function that is called for every element. In this function, you can extract the correct value from element that you want to find minimum of. Perfect use case for lambda.
key must be a callable taking one argument. Instead of comparing two items directly the results of key(item) are compared. However the actual items are left unmodified. key is only used to compute the comparison criteria. For numpy.min: axis specifies the index (or indices) of the axis (axes) that should be taken into account. See numpy.info(numpy.min) for more information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.