Skip to content

Fix update_or_create_hierarchy_from_dataframe fails when consolidated… #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TM1py/Services/HierarchyService.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def update_or_create_hierarchy_from_dataframe(
element_name: Element.Types(element_type)
for element_name, element_type
in df.loc[
~df[element_column].isin(existing_element_identifiers),
~df[element_column].str.lower().str.replace(' ', '').isin(existing_element_identifiers._store.keys()),
(element_column, element_type_column)
].itertuples(index=False)
})
Expand Down
48 changes: 43 additions & 5 deletions Tests/HierarchyService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,46 @@ def test_update_or_create_hierarchy_from_dataframe(self):
hierarchy_name=self.region_dimension_name)
self._verify_region_dimension(hierarchy)

def test_update_or_create_hierarchy_from_dataframe_consol_exists(self):
columns = [self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n", "level001",
"level000", "level001_weight", "level000_weight"]
data = [
['France', "Numeric", "Frankreich", "EUR", 60_000_000, "Europe", "World", 1, 1],
['Switzerland', 'Numeric', "Schweiz", "CHF", 9_000_000, "Europe", "World", 1, 1],
['Germany', 'Numeric', "Deutschland", "EUR", 84_000_000, "Europe", "World", 1, 1],
]
df = DataFrame(data=data, columns=columns)

self.tm1.hierarchies.update_or_create_hierarchy_from_dataframe(
dimension_name=self.region_dimension_name,
hierarchy_name=self.region_dimension_name,
df=df,
element_column=self.region_dimension_name,
element_type_column="ElementType",
unwind_all=True
)

data = [
['france', "Numeric", "Frankreich", "EUR", 60_000_000, "Europe", "World", 1, 1],
['switz erland', 'Numeric', "Schweiz", "CHF", 9_000_000, "Europe", "World", 1, 1],
['GerMANY', 'Numeric', "Deutschland", "EUR", 84_000_000, "Europe", "World", 1, 1],
]
df = DataFrame(data=data, columns=columns)

self.tm1.hierarchies.update_or_create_hierarchy_from_dataframe(
dimension_name=self.region_dimension_name,
hierarchy_name=self.region_dimension_name,
df=df,
element_column=self.region_dimension_name,
element_type_column="ElementType",
unwind_all=True
)

hierarchy = self.tm1.hierarchies.get(
dimension_name=self.region_dimension_name,
hierarchy_name=self.region_dimension_name)
self._verify_region_dimension(hierarchy)

def test_update_or_create_hierarchy_from_dataframe_non_standard_level_order(self):
columns = [self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n", "Level001",
"level000", "level001_weight", "level000_weight"]
Expand All @@ -524,8 +564,9 @@ def test_update_or_create_hierarchy_from_dataframe_non_standard_level_order(self
]
df = DataFrame(data=data, columns=columns)

df = pd.DataFrame(df[[self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n", "level000",
"Level001", "level000_weight", "level001_weight"]])
df = pd.DataFrame(
df[[self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n", "level000",
"Level001", "level000_weight", "level001_weight"]])

self.tm1.hierarchies.update_or_create_hierarchy_from_dataframe(
dimension_name=self.region_dimension_name,
Expand Down Expand Up @@ -1099,7 +1140,6 @@ def test_update_or_create_hierarchy_from_dataframe_unwind_consolidations_single(
unwind_all=True
)


columns = [self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n"]
data = [
['France', "Numeric", "Frankreich", "EUR", 60_000_000],
Expand Down Expand Up @@ -1128,7 +1168,6 @@ def test_update_or_create_hierarchy_from_dataframe_unwind_consolidations_single(
self.assertNotIn(("DACH", "Germany"), hierarchy.edges)
self.assertNotIn(("DACH", "Switzerland"), hierarchy.edges)


def test_update_or_create_hierarchy_from_dataframe_unwind_consolidations_multi(self):
columns = [self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n", "level002",
"level001", "level000", "level002_weight", "level001_weight", "level000_weight"]
Expand All @@ -1148,7 +1187,6 @@ def test_update_or_create_hierarchy_from_dataframe_unwind_consolidations_multi(s
unwind_all=True
)


columns = [self.region_dimension_name, "ElementType", "Alias:a", "Currency:s", "population:n"]
data = [
['France', "Numeric", "Frankreich", "EUR", 60_000_000],
Expand Down