Skip to content
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

CSVDataset behaves unexpectedly if src is a dataframe unexpected index #8201

Open
ashgillman opened this issue Nov 13, 2024 · 1 comment · May be fixed by #8351
Open

CSVDataset behaves unexpectedly if src is a dataframe unexpected index #8201

ashgillman opened this issue Nov 13, 2024 · 1 comment · May be fixed by #8351
Labels
good first issue Good for newcomers

Comments

@ashgillman
Copy link

ashgillman commented Nov 13, 2024

Describe the bug
CSVDataset accepts pandas DataFrames as input for src. But it makes assumptions about the index.

This is because convert_tables_to_dicts uses .loc instead of .iloc. It generates ordinal indexes to subset on but treats them as names indices.

data_ = df.loc[rows] if col_names is None else df.loc[rows, col_names]

To Reproduce

import numpy
import pandas
import monai

df = pandas.DataFrame(numpy.random.random((50, 3)))
df_subset = df.iloc[numpy.arange(0, 50, 5)]
print(df_subset.shape)  # (10, 3)

ds = monai.data.CSVDataset(df_subset)
print(len(ds))  # 3

Expected behavior
print(len(ds)) should return 10.
It returns 3 because it looks up indices slice(10), which match indices 0, 5 and 10 from the subset.

Environment
Shouldn't be relevant?

Additional context
Simple fix:

data_ = df.loc[rows] if col_names is None else df.loc[rows, col_names]

The first .loc should be .iloc, and the second should be .iloc[rows][col_names]

@ashgillman
Copy link
Author

Workaround is to always ".reset_index()" on src DataFrames.

@mingxin-zheng mingxin-zheng added the good first issue Good for newcomers label Feb 14, 2025
bartosz-grabowski added a commit to bartosz-grabowski/MONAI that referenced this issue Feb 18, 2025
@bartosz-grabowski bartosz-grabowski linked a pull request Feb 18, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants