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

refactor(python): deprecate iterrows in favour of iter_rows, add new @redirect class decorator #6461

Merged
merged 2 commits into from
Jan 26, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 26, 2023

Renames iterrows to iter_rows, consistent with the new iter_slices method.

  • Introduces new class decorator @redirect to help generically support a transition period for one or more renamed methods or attributes; triggers a DeprecationWarning on use, while transparently redirecting the underlying object's __getattribute__ to the new method.

  • Note that the redirecting code is only called if the requested method/attribute isn't found on the class (due to the differences in behaviour between __getattr__ and __getattribute__) so it adds zero overhead when invoking a method/attribute that exists.


New decorator usage:

@redirect({"iterrows": "iter_rows"})
class DataFrame:
    ...

Feels pretty clean; we can support both the new name ...

list( pl.DataFrame([1,2,3]).iter_rows() )
# [(1,), (2,), (3,)]

... and the old name (with an automatically-raised DeprecationWarning) ...

list( pl.DataFrame([1,2,3]).iterrows() )
# [(1,), (2,), (3,)]
#
# DeprecationWarning: `DataFrame.iterrows` has been renamed; this 
#   redirect is temporary, please use `.iter_rows` instead

... while having only the newer method visible on the class:

for attr in dir( pl.DataFrame ):
    if attr.startswith( "iter" ):
        print( attr )
        
# iter_rows
# iter_slices

@github-actions github-actions bot added python Related to Python Polars refactor Code improvements that do not change functionality labels Jan 26, 2023
@alexander-beedie alexander-beedie changed the title refactor(python): deprecate iterrows in favour of iter_rows refactor(python): deprecate iterrows in favour of iter_rows, add new @redirect class decorator Jan 26, 2023
@ritchie46 ritchie46 merged commit e9235ee into pola-rs:master Jan 26, 2023
@alexander-beedie alexander-beedie deleted the deprecate-iterrows branch January 26, 2023 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Related to Python Polars refactor Code improvements that do not change functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants