tm1py icon indicating copy to clipboard operation
tm1py copied to clipboard

Get_Elements_Dataframe fails when an attribute has the same name as the dimension/hierarchy

Open RobbyMeyers opened this issue 11 months ago • 2 comments

Using the Get_Elements_Dataframe function fails when there is an attribute with same name as the dimension. There is a uniqueness error produced.

ValueError: The column label 'Dimension Name' is not unique.

Because the attribute data is joined back to the element data there has to be a unique key/column (IE the dimension name column that holds the element names)

To Reproduce Create a dimension called MyDim and then create an attribute for elements in that dimension called MyDim.

Expected behavior I have modified the code to add a suffix to any attributes that have the same name as the dimension which solves the issue. Not sure if this is the best way to go about this or not.

Version TM1py 2.1 TM1 Server Version: 11.0.6.3

RobbyMeyers avatar Mar 07 '25 02:03 RobbyMeyers

Yes. That is a known problem.

TM1py can place the attribute type suffix or a custom prefix for you with these two arguments:

with TM1Service(**tm1_params) as tm1:
    df = tm1.elements.get_elements_dataframe(
        dimension_name="Account",
        hierarchy_name="Account",
        attribute_suffix=True)

    print(df.head().to_markdown())
Account Type Bootstrap:s Icon:s Color:s Type:s Order Adj:n Order Seed:n Element After:s Operator:s Account Type:s Description:a Format:s Picklist:s calculation:s crossdimcalc:s }FormatType:s level004_Weight level003_Weight level002_Weight level001_Weight level000_Weight level004 level003 level002 level001 level000
0 1110 Numeric N nan 4000 + Assets Cash 0 0 1 1 1 110 10 1
1 1130 Numeric N nan 6000 + Assets Trade Receivables 0 1 1 1 1 1120 110 10 1
2 1140 Numeric N nan 7000 + Assets Other Receivables 0 1 1 1 1 1120 110 10 1
3 1150 Numeric N nan 8000 + Assets Allowance for Bad Debt 0 0 1 1 1 110 10 1
4 1162 Numeric N nan 10000 + Assets Raw Materials 0 1 1 1 1 1160 110 10 1
with TM1Service(**tm1_params) as tm1:
    df = tm1.elements.get_elements_dataframe(
        dimension_name="Account",
        hierarchy_name="Account",
        attribute_column_prefix="Attribute - ")

    print(df.head().to_markdown())
Account Type Attribute - Bootstrap Attribute - Icon Attribute - Color Attribute - Type Attribute - Order Adj Attribute - Order Seed Attribute - Element After Attribute - Operator Attribute - Account Type Attribute - Description Attribute - Format Attribute - Picklist Attribute - calculation Attribute - crossdimcalc Attribute - }FormatType level004_Weight level003_Weight level002_Weight level001_Weight level000_Weight level004 level003 level002 level001 level000
0 1110 Numeric N nan 4000 + Assets Cash 0 0 1 1 1 110 10 1
1 1130 Numeric N nan 6000 + Assets Trade Receivables 0 1 1 1 1 1120 110 10 1
2 1140 Numeric N nan 7000 + Assets Other Receivables 0 1 1 1 1 1120 110 10 1
3 1150 Numeric N nan 8000 + Assets Allowance for Bad Debt 0 0 1 1 1 110 10 1
4 1162 Numeric N nan 10000 + Assets Raw Materials 0 1 1 1 1 1160 110 10 1

MariusWirtz avatar Mar 07 '25 08:03 MariusWirtz

I get this error still when trying to use your example using the current version of TM1py

tm1_target = TM1Service(address=target_srv, port=target_port, ssl=True, user=uid, password=pwd, namespace=namespace)


pd.options.display.width = 0

dim = 'MyDimName'

df = tm1_target.elements.get_elements_dataframe(
        dimension_name=dim,
        hierarchy_name=dim,
        attribute_column_prefix="Attribute - ")
print(df.head().to_markdown())
Traceback (most recent call last):
  File "C:\Users\rmeyers2\PycharmProjects\TM1_Project\Cascade_Data.py", line 28, in <module>
    df = tm1_target.elements.get_elements_dataframe(
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\TM1py\Utils\Utils.py", line 146, in wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\TM1py\Services\ElementService.py", line 541, in get_elements_dataframe
    return pd.merge(df, df_data, on=dimension_name).drop_duplicates()
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\pandas\core\reshape\merge.py", line 110, in merge
    op = _MergeOperation(
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\pandas\core\reshape\merge.py", line 703, in __init__
    ) = self._get_merge_keys()
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\pandas\core\reshape\merge.py", line 1162, in _get_merge_keys
    right_keys.append(right._get_label_or_level_values(rk))
  File "C:\Users\rmeyers2\AppData\Roaming\Python\Python310\site-packages\pandas\core\generic.py", line 1865, in _get_label_or_level_values
    raise ValueError(
ValueError: The column label 'My Dimension Name' is not unique.

Process finished with exit code 1

RobbyMeyers avatar Mar 10 '25 16:03 RobbyMeyers