Xarray : Operations with cubes with different granularities / levels same hierarchy / Multiindex











up vote
1
down vote

favorite












I am having trouble figuring out how to work with xarray DataArrays and DataSets and perform algebra operations; especially when the dimensions have different levels and my cubes have different granularities. I would be very grateful if someone could suggest me some documentation or give me some advice.



In the example below I am trying to compute the contribution of each child (SKU) under a parent (PFS). I found that to get the right values I need to convert the cube slice into a pandas dataframe. Otherwise, Xarray duplicates the dimension I am working with.



import pandas as pd
import numpy as np
import xarray as xr
from itertools import product

# Create hierachies

usage_type_entities = (('Regular',), ('Sample',),
('Tender',), ('Clinic Trial',))

usage_type_tree = pd.MultiIndex.from_tuples(
usage_type_entities, names=('Usage_Type',))

product_tree_hierarchy = (("PF1", "PFS1", "SKU1"),
("PF1", "PFS1", "SKU2"),
("PF1", "PFS2", "SKU3"),
("PF1", "PFS2", "SKU4"),
("PF2", "PFS3", "SKU5"))

product_tree_entities = ("PF", "PFS", "SKU")

product_tree = pd.MultiIndex.from_tuples(product_tree_hierarchy,
names=product_tree_entities)

market_tree_hierarchy = (("Group1", "Region1", "Market1"),
("Group1", "Region1", "Market2"),
("Group1", "Region2", "Market3"),
("Group1", "Region2", "Market4"),
("Group2", "Region3", "Market5"))

market_tree_entities = ("Groups", "Regions", "Markets")

market_tree = pd.MultiIndex.from_tuples(market_tree_hierarchy,
names=market_tree_entities)

time_tree_hierarchy = [(y, y+q) for y, q in product([str(2013+x) for x in range(6)],
["Q"+str(int(q)) for q in np.arange(1, 4.1, 1)])][0:22]

time_entities = ("Year", "Quarter")

time_tree = pd.MultiIndex.from_tuples(time_tree_hierarchy, names=time_entities)

# Create X-array Dataset

x1 = np.random.randint(100, size=(len(usage_type_tree), len(
product_tree), len(market_tree), len(time_tree)))

xda = xr.DataArray(x1, coords=(usage_type_tree, product_tree, market_tree, time_tree),
dims=("Usage", "Product", "Market", "Time"))

# Operations - I need to convert my slice into a pandas df to get
the right values. Converting to pandas df works ok.

market = "Market1"
ut = "Regular"

(xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas() /
xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas().groupby("PFS").sum(axis=0))


If I don't convert the slice to pandas df and I keep it as a xarray dataset, the dimension gets duplicated. For example, the line below produces a DatArray(Product: 5, Time: 22, PFS: 3), when it should be just (Product: 5, Time: 22)



(xda.sel(Markets=market, Usage_Type=ut)[:, 0] /
xda.sel(Markets=market, Usage_Type=ut)[:, 0].groupby("PFS").sum(axis=0))









share|improve this question


























    up vote
    1
    down vote

    favorite












    I am having trouble figuring out how to work with xarray DataArrays and DataSets and perform algebra operations; especially when the dimensions have different levels and my cubes have different granularities. I would be very grateful if someone could suggest me some documentation or give me some advice.



    In the example below I am trying to compute the contribution of each child (SKU) under a parent (PFS). I found that to get the right values I need to convert the cube slice into a pandas dataframe. Otherwise, Xarray duplicates the dimension I am working with.



    import pandas as pd
    import numpy as np
    import xarray as xr
    from itertools import product

    # Create hierachies

    usage_type_entities = (('Regular',), ('Sample',),
    ('Tender',), ('Clinic Trial',))

    usage_type_tree = pd.MultiIndex.from_tuples(
    usage_type_entities, names=('Usage_Type',))

    product_tree_hierarchy = (("PF1", "PFS1", "SKU1"),
    ("PF1", "PFS1", "SKU2"),
    ("PF1", "PFS2", "SKU3"),
    ("PF1", "PFS2", "SKU4"),
    ("PF2", "PFS3", "SKU5"))

    product_tree_entities = ("PF", "PFS", "SKU")

    product_tree = pd.MultiIndex.from_tuples(product_tree_hierarchy,
    names=product_tree_entities)

    market_tree_hierarchy = (("Group1", "Region1", "Market1"),
    ("Group1", "Region1", "Market2"),
    ("Group1", "Region2", "Market3"),
    ("Group1", "Region2", "Market4"),
    ("Group2", "Region3", "Market5"))

    market_tree_entities = ("Groups", "Regions", "Markets")

    market_tree = pd.MultiIndex.from_tuples(market_tree_hierarchy,
    names=market_tree_entities)

    time_tree_hierarchy = [(y, y+q) for y, q in product([str(2013+x) for x in range(6)],
    ["Q"+str(int(q)) for q in np.arange(1, 4.1, 1)])][0:22]

    time_entities = ("Year", "Quarter")

    time_tree = pd.MultiIndex.from_tuples(time_tree_hierarchy, names=time_entities)

    # Create X-array Dataset

    x1 = np.random.randint(100, size=(len(usage_type_tree), len(
    product_tree), len(market_tree), len(time_tree)))

    xda = xr.DataArray(x1, coords=(usage_type_tree, product_tree, market_tree, time_tree),
    dims=("Usage", "Product", "Market", "Time"))

    # Operations - I need to convert my slice into a pandas df to get
    the right values. Converting to pandas df works ok.

    market = "Market1"
    ut = "Regular"

    (xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas() /
    xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas().groupby("PFS").sum(axis=0))


    If I don't convert the slice to pandas df and I keep it as a xarray dataset, the dimension gets duplicated. For example, the line below produces a DatArray(Product: 5, Time: 22, PFS: 3), when it should be just (Product: 5, Time: 22)



    (xda.sel(Markets=market, Usage_Type=ut)[:, 0] /
    xda.sel(Markets=market, Usage_Type=ut)[:, 0].groupby("PFS").sum(axis=0))









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am having trouble figuring out how to work with xarray DataArrays and DataSets and perform algebra operations; especially when the dimensions have different levels and my cubes have different granularities. I would be very grateful if someone could suggest me some documentation or give me some advice.



      In the example below I am trying to compute the contribution of each child (SKU) under a parent (PFS). I found that to get the right values I need to convert the cube slice into a pandas dataframe. Otherwise, Xarray duplicates the dimension I am working with.



      import pandas as pd
      import numpy as np
      import xarray as xr
      from itertools import product

      # Create hierachies

      usage_type_entities = (('Regular',), ('Sample',),
      ('Tender',), ('Clinic Trial',))

      usage_type_tree = pd.MultiIndex.from_tuples(
      usage_type_entities, names=('Usage_Type',))

      product_tree_hierarchy = (("PF1", "PFS1", "SKU1"),
      ("PF1", "PFS1", "SKU2"),
      ("PF1", "PFS2", "SKU3"),
      ("PF1", "PFS2", "SKU4"),
      ("PF2", "PFS3", "SKU5"))

      product_tree_entities = ("PF", "PFS", "SKU")

      product_tree = pd.MultiIndex.from_tuples(product_tree_hierarchy,
      names=product_tree_entities)

      market_tree_hierarchy = (("Group1", "Region1", "Market1"),
      ("Group1", "Region1", "Market2"),
      ("Group1", "Region2", "Market3"),
      ("Group1", "Region2", "Market4"),
      ("Group2", "Region3", "Market5"))

      market_tree_entities = ("Groups", "Regions", "Markets")

      market_tree = pd.MultiIndex.from_tuples(market_tree_hierarchy,
      names=market_tree_entities)

      time_tree_hierarchy = [(y, y+q) for y, q in product([str(2013+x) for x in range(6)],
      ["Q"+str(int(q)) for q in np.arange(1, 4.1, 1)])][0:22]

      time_entities = ("Year", "Quarter")

      time_tree = pd.MultiIndex.from_tuples(time_tree_hierarchy, names=time_entities)

      # Create X-array Dataset

      x1 = np.random.randint(100, size=(len(usage_type_tree), len(
      product_tree), len(market_tree), len(time_tree)))

      xda = xr.DataArray(x1, coords=(usage_type_tree, product_tree, market_tree, time_tree),
      dims=("Usage", "Product", "Market", "Time"))

      # Operations - I need to convert my slice into a pandas df to get
      the right values. Converting to pandas df works ok.

      market = "Market1"
      ut = "Regular"

      (xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas() /
      xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas().groupby("PFS").sum(axis=0))


      If I don't convert the slice to pandas df and I keep it as a xarray dataset, the dimension gets duplicated. For example, the line below produces a DatArray(Product: 5, Time: 22, PFS: 3), when it should be just (Product: 5, Time: 22)



      (xda.sel(Markets=market, Usage_Type=ut)[:, 0] /
      xda.sel(Markets=market, Usage_Type=ut)[:, 0].groupby("PFS").sum(axis=0))









      share|improve this question













      I am having trouble figuring out how to work with xarray DataArrays and DataSets and perform algebra operations; especially when the dimensions have different levels and my cubes have different granularities. I would be very grateful if someone could suggest me some documentation or give me some advice.



      In the example below I am trying to compute the contribution of each child (SKU) under a parent (PFS). I found that to get the right values I need to convert the cube slice into a pandas dataframe. Otherwise, Xarray duplicates the dimension I am working with.



      import pandas as pd
      import numpy as np
      import xarray as xr
      from itertools import product

      # Create hierachies

      usage_type_entities = (('Regular',), ('Sample',),
      ('Tender',), ('Clinic Trial',))

      usage_type_tree = pd.MultiIndex.from_tuples(
      usage_type_entities, names=('Usage_Type',))

      product_tree_hierarchy = (("PF1", "PFS1", "SKU1"),
      ("PF1", "PFS1", "SKU2"),
      ("PF1", "PFS2", "SKU3"),
      ("PF1", "PFS2", "SKU4"),
      ("PF2", "PFS3", "SKU5"))

      product_tree_entities = ("PF", "PFS", "SKU")

      product_tree = pd.MultiIndex.from_tuples(product_tree_hierarchy,
      names=product_tree_entities)

      market_tree_hierarchy = (("Group1", "Region1", "Market1"),
      ("Group1", "Region1", "Market2"),
      ("Group1", "Region2", "Market3"),
      ("Group1", "Region2", "Market4"),
      ("Group2", "Region3", "Market5"))

      market_tree_entities = ("Groups", "Regions", "Markets")

      market_tree = pd.MultiIndex.from_tuples(market_tree_hierarchy,
      names=market_tree_entities)

      time_tree_hierarchy = [(y, y+q) for y, q in product([str(2013+x) for x in range(6)],
      ["Q"+str(int(q)) for q in np.arange(1, 4.1, 1)])][0:22]

      time_entities = ("Year", "Quarter")

      time_tree = pd.MultiIndex.from_tuples(time_tree_hierarchy, names=time_entities)

      # Create X-array Dataset

      x1 = np.random.randint(100, size=(len(usage_type_tree), len(
      product_tree), len(market_tree), len(time_tree)))

      xda = xr.DataArray(x1, coords=(usage_type_tree, product_tree, market_tree, time_tree),
      dims=("Usage", "Product", "Market", "Time"))

      # Operations - I need to convert my slice into a pandas df to get
      the right values. Converting to pandas df works ok.

      market = "Market1"
      ut = "Regular"

      (xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas() /
      xda.sel(Markets=market, Usage_Type=ut)[:, 0].to_pandas().groupby("PFS").sum(axis=0))


      If I don't convert the slice to pandas df and I keep it as a xarray dataset, the dimension gets duplicated. For example, the line below produces a DatArray(Product: 5, Time: 22, PFS: 3), when it should be just (Product: 5, Time: 22)



      (xda.sel(Markets=market, Usage_Type=ut)[:, 0] /
      xda.sel(Markets=market, Usage_Type=ut)[:, 0].groupby("PFS").sum(axis=0))






      python-xarray






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:38









      Joan Ponsa-Cobas

      61




      61





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250863%2fxarray-operations-with-cubes-with-different-granularities-levels-same-hierar%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250863%2fxarray-operations-with-cubes-with-different-granularities-levels-same-hierar%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues