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/3984#issuecomment-619635716,https://api.github.com/repos/pydata/xarray/issues/3984,619635716,MDEyOklzc3VlQ29tbWVudDYxOTYzNTcxNg==,1559890,2020-04-26T22:38:20Z,2020-04-26T22:38:20Z,NONE,Your both methods worked! Thank you!,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,602793814
https://github.com/pydata/xarray/issues/1569#issuecomment-488112182,https://api.github.com/repos/pydata/xarray/issues/1569,488112182,MDEyOklzc3VlQ29tbWVudDQ4ODExMjE4Mg==,1559890,2019-04-30T20:55:01Z,2019-04-30T20:55:01Z,NONE,"I got a solution which is not so flexible but works:
Data:
```python
import pandas as pd
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo']),
np.array(['one', 'two', 'one', 'two', 'one', 'two'])]
da = pd.DataFrame(np.random.randn(6, 4)).to_xarray().to_array()
da.coords['index'] = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
da
```
```
array([[ 0.379189, 1.082292, -2.073478, -0.84626 , -1.529927, -0.837407],
[-0.267983, -0.2516 , -1.016653, -0.085762, -0.058382, -0.667891],
[-0.013488, -0.855332, -0.038072, -0.385211, -2.149742, -0.304361],
[ 1.749561, -0.606031, 1.914146, 1.6292 , -0.515519, 1.996283]])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'bar' 'baz' 'baz' 'foo' 'foo'
- second (index) object 'one' 'two' 'one' 'two' 'one' 'two'
* variable (variable) int32 0 1 2 3
```
Groupby on levels:
```python
def multi_groupby(data, levels):
return (((k0,k1),v1) for k0,v0 in data.groupby(levels[0]) for k1,v1 in v0.groupby(levels[1]))
for k,v in multi_groupby(da,['second','variable']):
print(k,v)
```
```
('one', 0)
array([ 0.379189, -2.073478, -1.529927])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'one' 'one' 'one'
variable int32 0
('one', 1)
array([-0.267983, -1.016653, -0.058382])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'one' 'one' 'one'
variable int32 1
('one', 2)
array([-0.013488, -0.038072, -2.149742])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'one' 'one' 'one'
variable int32 2
('one', 3)
array([ 1.749561, 1.914146, -0.515519])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'one' 'one' 'one'
variable int32 3
('two', 0)
array([ 1.082292, -0.84626 , -0.837407])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'two' 'two' 'two'
variable int32 0
('two', 1)
array([-0.2516 , -0.085762, -0.667891])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'two' 'two' 'two'
variable int32 1
('two', 2)
array([-0.855332, -0.385211, -0.304361])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'two' 'two' 'two'
variable int32 2
('two', 3)
array([-0.606031, 1.6292 , 1.996283])
Coordinates:
* index (index) MultiIndex
- first (index) object 'bar' 'baz' 'foo'
- second (index) object 'two' 'two' 'two'
variable int32 3
```","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,257070215