Skip to content

Commit

Permalink
alter flatten function to work on dict only. not a list of dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeSBarros committed Feb 13, 2024
1 parent b7aee6c commit 1938c31
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions crossfire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def flatten(data, nested_columns=None):
nested_columns = set(nested_columns or NESTED_COLUMNS)
if not nested_columns.issubset(NESTED_COLUMNS):
raise NestedColumnError(nested_columns)
if isinstance(data, list):
keys = set(data[0].keys()) & nested_columns
for item in data:
for key in keys:
for k, v in item.get(key).items():
return f"{key}_{k}", v
if isinstance(data, dict):
keys = set(data.keys()) & nested_columns
flattened_dict = {}
for key in keys:
for k, v in data.get(key).items():
flattened_dict.update({f"{key}_{k}": v})
return flattened_dict
12 changes: 6 additions & 6 deletions tests/test_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

from crossfire import NestedColumnError, flatten

DICT_DATA = [
{"answer": 42, "contextInfo": {"context1": "info1", "context2": "info2"}}
]
PD_DATA = DataFrame(DICT_DATA)
DICT_DATA = {
"answer": 42,
"contextInfo": {"context1": "info1", "context2": "info2"},
}
PD_DATA = DataFrame([DICT_DATA])
GEOMETRY = [Point(4, 2)]
GEOPD_DATA = GeoDataFrame(DICT_DATA, crs="EPSG:4326", geometry=GEOMETRY)
GEOPD_DATA = GeoDataFrame([DICT_DATA], crs="EPSG:4326", geometry=GEOMETRY)


def teste_flatten_wrong_nested_columns_value_error():
Expand All @@ -24,7 +25,6 @@ def test_flatten_dict():
DICT_DATA, nested_columns=["contextInfo", "transports"]
)
assert flattened_dict == {
"answer": 42,
"contextInfo_context1": "info1",
"contextInfo_context2": "info2",
}

0 comments on commit 1938c31

Please # to comment.