html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/2450#issuecomment-426421471,https://api.github.com/repos/pydata/xarray/issues/2450,426421471,MDEyOklzc3VlQ29tbWVudDQyNjQyMTQ3MQ==,41115380,2018-10-02T20:38:37Z,2018-10-02T20:38:37Z,NONE,"@spencerkclark yeah I was using a different platform, updated my xarray version and this works now, thanks!","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426398519,https://api.github.com/repos/pydata/xarray/issues/2450,426398519,MDEyOklzc3VlQ29tbWVudDQyNjM5ODUxOQ==,6628425,2018-10-02T19:21:45Z,2018-10-02T19:21:45Z,MEMBER,"This seems like an xarray version issue; the `other` argument was not added to `where` until [version 0.10.0](http://xarray.pydata.org/en/stable/whats-new.html#id117). Are you running this script on a different platform than before? I'm surprised your original code,
https://github.com/pydata/xarray/issues/2450#issuecomment-426346137, didn't raise the same error.
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426394309,https://api.github.com/repos/pydata/xarray/issues/2450,426394309,MDEyOklzc3VlQ29tbWVudDQyNjM5NDMwOQ==,41115380,2018-10-02T19:07:37Z,2018-10-02T19:07:37Z,NONE,"@spencerkclark
```python
Traceback (most recent call last):
File ""/var/spool/torque/mom_priv/jobs/63347.rdf-xcat.SC"", line 22, in
a = ml50_shear[0].where(ml50_shear[0] >= 11, other = 0)
NotImplementedError: The optional argument 'other' has not yet been implemented
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426392397,https://api.github.com/repos/pydata/xarray/issues/2450,426392397,MDEyOklzc3VlQ29tbWVudDQyNjM5MjM5Nw==,6628425,2018-10-02T19:01:16Z,2018-10-02T19:01:16Z,MEMBER,"> Gonna try with just the first of the four
👍 let me know how it goes.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426384000,https://api.github.com/repos/pydata/xarray/issues/2450,426384000,MDEyOklzc3VlQ29tbWVudDQyNjM4NDAwMA==,41115380,2018-10-02T18:35:32Z,2018-10-02T18:47:07Z,NONE,"@spencerkclark Oh you're right, I didn't think about that - it returns 4
Gonna try with just the first of the four","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426381265,https://api.github.com/repos/pydata/xarray/issues/2450,426381265,MDEyOklzc3VlQ29tbWVudDQyNjM4MTI2NQ==,6628425,2018-10-02T18:27:17Z,2018-10-02T18:27:17Z,MEMBER,Does `complete_shear` happen to return more than one DataArray?,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426377815,https://api.github.com/repos/pydata/xarray/issues/2450,426377815,MDEyOklzc3VlQ29tbWVudDQyNjM3NzgxNQ==,41115380,2018-10-02T18:17:10Z,2018-10-02T18:17:10Z,NONE,"@spencerkclark ah sorry I missed line 5 - I'm not looking to get the mean, I want to make sure the unusual values are still represented. That might be why mine went wrong, it generally only works when there's just one time. Anyway, here's what I tried:
```python
import xarray as xr
from function_codes import sfcwinds, complete_shear, winds
import os
pathway = #my pathway
os.chdir(pathway)
sfc_winds = sfcwinds(""sfc"")
ml50_winds = winds(""ml50"")
ml50_shear = complete_shear(sfc_winds, ml50_winds)
a = ml50_shear.where(ml50_shear >= 11, other = 0)
b = a.where(a == 0, other = 1)
pa = (b.sum('time') / b.sizes['time']) * 100
print(pa)
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426374411,https://api.github.com/repos/pydata/xarray/issues/2450,426374411,MDEyOklzc3VlQ29tbWVudDQyNjM3NDQxMQ==,6628425,2018-10-02T18:07:16Z,2018-10-02T18:07:16Z,MEMBER,"@am-thyst could you show all the code that you used to try it out? If I run all the lines (from 1-8 in
https://github.com/pydata/xarray/issues/2450#issuecomment-426340226 and https://github.com/pydata/xarray/issues/2450#issuecomment-426353764) in my IPython console things work for me.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426371853,https://api.github.com/repos/pydata/xarray/issues/2450,426371853,MDEyOklzc3VlQ29tbWVudDQyNjM3MTg1Mw==,41115380,2018-10-02T17:59:47Z,2018-10-02T17:59:47Z,NONE,"@spencerkclark following your method:
```python
AttributeError: 'tuple' object has no attribute 'where'
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426354773,https://api.github.com/repos/pydata/xarray/issues/2450,426354773,MDEyOklzc3VlQ29tbWVudDQyNjM1NDc3Mw==,41115380,2018-10-02T17:10:45Z,2018-10-02T17:10:45Z,NONE,"@spencerkclark Thanks a lot, I'll give it a try 👍 ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426353764,https://api.github.com/repos/pydata/xarray/issues/2450,426353764,MDEyOklzc3VlQ29tbWVudDQyNjM1Mzc2NA==,6628425,2018-10-02T17:07:37Z,2018-10-02T17:07:37Z,MEMBER,"@am-thyst thanks for the extra details -- try out my method -- I think it does the same thing. Well perhaps you'll just need to multiply the result by 100 at the end and use `>=` instead of `>` for the threshold condition :)
Your approach can also be vectorized in such a way that you don't need to manually iterate over the individual times:
```
In [6]: b = da.where(da >= threshold, other=0)
In [7]: c = b.where(b == 0, other=1)
In [8]: (c.sum('time') / c.sizes['time']) * 100
Out[8]:
array([[ 25., 50., 25.],
[ 25., 50., 50.]])
Dimensions without coordinates: x, y
```
Effectively `da >= threshold` in my earlier example is shorthand for lines 6 and 7 from the example above (and taking the mean is the same as summing over the dimension and dividing by the number of elements along that dimension).","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426346137,https://api.github.com/repos/pydata/xarray/issues/2450,426346137,MDEyOklzc3VlQ29tbWVudDQyNjM0NjEzNw==,41115380,2018-10-02T16:45:17Z,2018-10-02T16:45:17Z,NONE,"@spencerkclark
```python
a = da.loc[date[0]] #the initial time, obviously this part doesn't work but this is the methodology
b = a.where(a >= threshold, other = 0) #keeps values that satisfy the threshold, sets the rest to 0
c = b.where(b == 0, other = 1) #sets all values that satisfy the threshold to 1
new_array = zero_array + c #zero_array is just an array of the same shape without time, set to 0
runs = runs + 1
```
Loops through this whole thing for every time (changing the loc to date[1], date[2], etc.), then:
```python
percentage = (new_array / runs) * 100
```
This is not my complete code but you should get the idea, as I said it works fine with only 1 time per day","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426340226,https://api.github.com/repos/pydata/xarray/issues/2450,426340226,MDEyOklzc3VlQ29tbWVudDQyNjM0MDIyNg==,6628425,2018-10-02T16:28:42Z,2018-10-02T16:28:42Z,MEMBER,"I'm not sure if I'm understanding the calculation correctly; is something like the following what you are looking for?
```
In [1]: import numpy as np; import pandas as pd; import xarray as xr
In [2]: times = pd.date_range('1979', periods=4, freq='12H')
In [3]: da = xr.DataArray(np.random.rand(4, 2, 3), coords={'time': times}, dims=['time', 'x', 'y'])
In [4]: threshold = 0.5
In [5]: (da > threshold).mean('time')
Out[5]:
array([[ 0.25, 0.5 , 0.25],
[ 0.25, 0.5 , 0.5 ]])
Dimensions without coordinates: x, y
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426329598,https://api.github.com/repos/pydata/xarray/issues/2450,426329598,MDEyOklzc3VlQ29tbWVudDQyNjMyOTU5OA==,41115380,2018-10-02T15:58:21Z,2018-10-02T15:58:21Z,NONE,"@spencerkclark I'm creating a 2D array of the percentage of time each lon/lat coordinate exceeds a defined threshold. I did this fine before, but I was only using 1 time from each day","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426317958,https://api.github.com/repos/pydata/xarray/issues/2450,426317958,MDEyOklzc3VlQ29tbWVudDQyNjMxNzk1OA==,6628425,2018-10-02T15:27:59Z,2018-10-02T15:36:28Z,MEMBER,@am-thyst could you provide more details on what sort of operation you are trying to do with each time slice? Typically this kind of process can be better handled in a vectorized way (i.e. without manually iterating over the times).,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426316501,https://api.github.com/repos/pydata/xarray/issues/2450,426316501,MDEyOklzc3VlQ29tbWVudDQyNjMxNjUwMQ==,41115380,2018-10-02T15:24:21Z,2018-10-02T15:24:21Z,NONE,"@spencerkclark thanks for this, using '1979-01-01T00' works which is a relief. I am trying to use the method of date[number] because I have in excess of 4,000 times to go through. Is there a way to get it to work? I figured adding the %H would work, but obviously not!
Otherwise, at least I know I can revise my code to state the datetime explicitly. ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396
https://github.com/pydata/xarray/issues/2450#issuecomment-426250976,https://api.github.com/repos/pydata/xarray/issues/2450,426250976,MDEyOklzc3VlQ29tbWVudDQyNjI1MDk3Ng==,6628425,2018-10-02T12:16:03Z,2018-10-02T12:16:03Z,MEMBER,"Thanks @am-thyst, this happens to be expected behavior. For a dimension indexed by a DatetimeIndex (e.g. your `'time'` dimension), label-based indexing with strings uses so-called [""partial string indexing""](https://pandas.pydata.org/pandas-docs/stable/timeseries.html#partial-string-indexing). This behavior in xarray is inherited from pandas. Essentially what it means is that if you provide a string to a label-based indexer (like `.loc`), then it will return values from all times that would begin with the string you provided if expressed in string form. So, in your example, if you provide only the year, month, and day (e.g. `'1979-01-01'`) it will return values from `'1979-01-01'` and `1979-01-01T12:00:00`, because both string representations start with `'1979-01-01'`.
As you inferred, if you add detail to the string you provide, you can refine the selection. That said, you need to be careful of the format you use (I think the reason you get a `KeyError` in your second example is that pandas does not know how to interpret the string you provided). While pandas has some flexibility in how it parses datetime-strings I might recommend sticking to the [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601), which should work in all cases. For example to select just the values associated with the first date in your array you could do something like this:
```
In [1]: import numpy as np; import pandas as pd; import xarray as xr
In [2]: times = pd.date_range('1979', periods=8, freq='12H')
In [3]: da = xr.DataArray(np.arange(8), coords=[times], dims=['time'])
In [4]: da
Out[4]:
array([0, 1, 2, 3, 4, 5, 6, 7])
Coordinates:
* time (time) datetime64[ns] 1979-01-01 ... 1979-01-04T12:00:00
In [5]: da.loc['1979-01-01T00']
Out[5]:
array(0)
Coordinates:
time datetime64[ns] 1979-01-01
```
One other thing to note, in general, is that you can pass datetimes themselves to label-based indexers if you want to select specific dates; this way you don't need to go through the intermediate step of converting a date to a string:
```
In [6]: da.loc[datetime(1979, 1, 1)]
Out[6]:
array(0)
Coordinates:
time datetime64[ns] 1979-01-01
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,365438396