Skip to content

Commit ee6fb86

Browse files
Fix for indexing in convert_tables_to_dicts() which also works with row_indices != None
By changing .loc to .iloc, the previous fix didn't work for row_indices != None and df subsets, because it assumed positional index instead of label index. Signed-off-by: Bartosz Grabowski <[email protected]>
1 parent 5611e21 commit ee6fb86

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

monai/data/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ def convert_tables_to_dicts(
14731473
# parse row indices
14741474
rows: list[int | str] = []
14751475
if row_indices is None:
1476-
rows = slice(df.shape[0]) # type: ignore
1476+
rows = df.index.tolist()
14771477
else:
14781478
for i in row_indices:
14791479
if isinstance(i, (tuple, list)):
@@ -1484,7 +1484,7 @@ def convert_tables_to_dicts(
14841484
rows.append(i)
14851485

14861486
# convert to a list of dictionaries corresponding to every row
1487-
data_ = df.iloc[rows] if col_names is None else df.iloc[rows][col_names]
1487+
data_ = df.loc[rows] if col_names is None else df.loc[rows, col_names]
14881488
if isinstance(col_types, dict):
14891489
# fill default values for NaN
14901490
defaults = {k: v["default"] for k, v in col_types.items() if v is not None and v.get("default") is not None}
@@ -1500,7 +1500,7 @@ def convert_tables_to_dicts(
15001500
if col_groups is not None:
15011501
groups: dict[str, list] = {}
15021502
for name, cols in col_groups.items():
1503-
groups[name] = df.iloc[rows][cols].values
1503+
groups[name] = df.loc[rows, cols].values
15041504
# invert items of groups to every row of data
15051505
data = [dict(d, **{k: v[i] for k, v in groups.items()}) for i, d in enumerate(data)]
15061506

0 commit comments

Comments
 (0)