cubewise-code / tm1py

TM1py is a Python package that wraps the TM1 REST API in a simple to use library.
http://tm1py.readthedocs.io/en/latest/
MIT License
187 stars 107 forks source link

optional `fixed_elements` and `determine_order` arguments in `write_dataframe` function #1140

Open MariusWirtz opened 3 weeks ago

MariusWirtz commented 3 weeks ago

Currently, the write_dataframe function expects the data frame columns to match the dimension order perfectly in the cube. For example, for a cube with five dimensions (Version, Time, Period, Product, Measure), the function expects a data frame with six columns (Version, Time, Period, Product, Measure, Value) in the correct order.

It would be convenient if TM1py could order the columns itself, and the user would not need to ensure the column order. That approach would also allow to pass contexts / fixed elements to be handled more efficiently. They could be passed as a dictionary, e.g., {'Version': 'Actual'}.

When these new arguments are used, the column header must match the dimension name so that TM1py can order accordingly.

Kevin-Dekker commented 3 weeks ago

Sounds good Marius, that will benefit me too.

Related to the suggested features: perhaps an option to have one dimension's elements on the columns would be nice to much like execute_mdx_dataframe_shaped. That would save people a melt.

E.g. Sales Cube below

Version Product Sales Value Sales Quantity Sales Price
ACT 123 320.4 10 32.4
ACT 123 32.32 8 4.04
ACT 123 10.3 1 10.3

You currently have to share it yourself like this (unless I missed any current feature):

Version Product Sales Measure Value
ACT 123 Sales Value 320.4
ACT 123 Sales Quantity 10
ACT 123 Sales Price 32.4
ACT 123 ... ...

A few ideas to realize this:

  1. with argument column_dimension we melt the df with:

    • id_vars = [col for col in columns if col in dimensions],
    • var_name=column_dimension.
    • value_vars =[col for col in columns if col not in dimensions].
  2. with the argument shaped=True the column_dimension will be inferred? var_name = [dim for dim in dimensions if dim not in columns][0] <- with an exception if you end up with len([dim for dim in dimensions if dim not in columns][0]) != 1

  3. allow to pass the melt parameters to write_dataframe.