Skip to content

Commit

Permalink
Merge pull request #140 from K4liber/bug/unit_map_order
Browse files Browse the repository at this point in the history
Fixes #141 
---------

Co-authored-by: Jan Bielecki <[email protected]>
  • Loading branch information
guilhermebs and Jan Bielecki authored Oct 16, 2023
2 parents 00b6788 + 94a53ae commit 549a87c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pdtable/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ def make_table_dataframe(
if units is not None:
columns = {col_name: ColumnMetadata(unit) for col_name, unit in zip(df.columns, units)}
elif unit_map is not None:
for col, unit in unit_map.items():
columns[col] = ColumnMetadata(unit)
for col in df.columns:
if col in unit_map:
columns[col] = ColumnMetadata(unit_map[col])
else:
warnings.warn(f'Missing unit for column "{col}".')

table_info = ComplementaryTableInfo(table_metadata=table_metadata, columns=columns)
df = TableDataFrame.from_table_info(
Expand Down
17 changes: 17 additions & 0 deletions pdtable/test/test_pdtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,20 @@ def test_table__str_destination_with_no_spaces_results_in_single_destination():
def test_table__str_destination_with_spaces_results_in_multiple_destinations():
table = Table(name="test", destinations="a b c")
assert table.destinations == {"a", "b", "c"}


def test_unit_map_with_different_order_than_columns(tmpdir):
data_frame = pd.DataFrame.from_dict({
'column_text': ['a', 'b', 'c'],
'column_deg': [1, 2, 3]
})
table = frame.make_table_dataframe(
df=data_frame,
name='test_unit_map',
unit_map={
'column_deg': 'deg',
'column_text': 'text'
},
)
assert {'column_text': 'text', 'column_deg': 'deg'} == \
dict(zip(table.columns, frame.get_table_info(df=table).units))

0 comments on commit 549a87c

Please sign in to comment.