home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 233904555

This data as json

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/906#issuecomment-233904555 https://api.github.com/repos/pydata/xarray/issues/906 233904555 MDEyOklzc3VlQ29tbWVudDIzMzkwNDU1NQ== 6213168 2016-07-20T09:52:42Z 2016-07-20T09:52:42Z MEMBER

This preamble should be integrated inside unstack():

``` python import operator from functools import reduce

def proper_unstack(array, dim):

# Regenerate Pandas multi-index to be ordered by appearance
# TODO: check that the stacked coords repeat periodically
# TODO: write a faster/cleaner algorithm using numpy
mindex = array.coords[dim].to_pandas().index

levels = []
labels = []
for dim_i, (levels_i, labels_i) in enumerate(zip(mindex.levels, mindex.labels)):
    step_inner = reduce(operator.mul, (len(lvl) for lvl in mindex.levels[dim_i + 1:]), 1)
    step_outer = reduce(operator.mul, (len(lvl) for lvl in mindex.levels[:dim_i]), 1)

    levels.append([levels_i[labels_i[j]] for j in range(0, levels_i.size * step_inner, step_inner)])
    labels.append(reduce(operator.add, ([j] * step_inner for j in range(levels_i.size))) * step_outer)

mindex = pandas.MultiIndex(levels, labels, names=mindex.names)
array = array.copy()
array.coords[dim] = mindex
return array.unstack(dim)

proper_unstack(a, 'dim_0') ```

<xarray.DataArray (x: 2, count: 4)> array([[0, 1, 2, 3], [4, 5, 6, 7]]) Coordinates: * x (x) object 'x1' 'x0' * count (count) object 'first' 'second' 'third' 'fourth'

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  166439490
Powered by Datasette · Queries took 0.956ms · About: xarray-datasette