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

Optionally ignore indices in all.equal #6134

Open
mb706 opened this issue May 13, 2024 · 1 comment · May be fixed by #6136
Open

Optionally ignore indices in all.equal #6134

mb706 opened this issue May 13, 2024 · 1 comment · May be fixed by #6136

Comments

@mb706
Copy link

mb706 commented May 13, 2024

Different tables that contain the same data and also behave pretty much the same will not be all.equal() when they have different indices. Indices can sometimes appear automatically depending on what operations are performed with tables, so I think it can often be reasonable to ignore differences in indices.

x <- data.table(a = 1)
y <- data.table(a = 1)
all.equal(x, y)
#> [1] TRUE
x[a == 1]
#>        a
#>    <num>
#> 1:     1
all.equal(x, y)  # would be nice to have a way to get 'TRUE' here
#> [1] "Datasets have different indices. 'target': [a]. 'current': has no index."

While one can set check.attributes = FALSE, this also ignores other more important things like column names

all.equal(x, y, check.attributes = FALSE)
#> [1] TRUE
all.equal(x, data.table(b = 1), check.attributes = FALSE)  # this is too lenient
#> [1] TRUE

The user can set / unset indices in tables before checking with all.equals(), but that gets unnecessarily complicated when the tables are buried inside larger objects.

It would therefore be useful to have an option check.indices or ignore.indices that makes all.equal() ignore data.table indices.

My use case is that I use all.equal.data.table a lot in unit tests, where I verify that objects created in different ways still contain the same data. Tables may have different indices depending on how objects are created, but because these indices make no relevant difference in object behaviour I would like to ignore them.

@tdhock
Copy link
Member

tdhock commented May 14, 2024

a work-around you can use with current code is to just copy the data tables, data.table(your_dt_with_index) which will remove the index I believe.
this functionality seems reasonable to me, so if you could draft a PR, it would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants