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

Add precondition check for SortHead #3

Open
baziotis opened this issue Apr 5, 2023 · 0 comments
Open

Add precondition check for SortHead #3

baziotis opened this issue Apr 5, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@baziotis
Copy link
Collaborator

baziotis commented Apr 5, 2023

The transformation <anything>.sort_values().head() --> <anything>.nsmallest(n=5) is correct only if <anything> is a pandas.Series (it also works for DataFrames but we don't support it for now). Currently, we don't perform this check.

In the following example, Dias incorrectly rewrites code:

import pandas as pd
import dias.rewriter
class Foo:
    def sort_values(self):
        return self
    def head(self):
        return self
----- NEW CELL ----
%%rewrite
foo = Foo()
foo.sort_values().head()

Add the check into the rewritten code. So, if the original code is res = <anything>.sort_values().head(), the rewritten code should be:

_REWR_tmp0 = <anything>
if isinstance(_REWR_tmp):
  _REWR_tmp1 = _REWR_tmp0.nsmallest(n=5)
else:
  _REWR_tmp1 = _REWR_tmp0.sort_values().head()
res = _REWR_tmp1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant