API: add a setattr method to NDFrame to assist with piping
See original GitHub issueI frequently come into situations where I have to break up piping, because piping doesn’t work on attributes:
>>> new_df = (df.groupby(...)
... .pipe(...)
>>> new_df.index.name = 'index_name'
>>> new_df.columns = pd.CategoricalIndex(new_df.columns)
>>> new_df = new_df.pipe(...) # and so on...
I think it would be cleaner if NDFrame had a setattr
method that returns self
. Then the above would become:
>>> new_df = (df.groupby(...)
... .pipe(...)
... .setattr('index.name', 'index_name')
... .setattr('columns', lambda x: pd.CategoricalIndex(x.columns))
... .pipe(...) # and so on...
... )
The first argument to .setattr
is a string and can be dot-seperated, and the second parameter is called with self
as its first argument if a callable.
I think this would make piping cleaner in many cases.
Opinions?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Python setattr() method - GeeksforGeeks
Python setattr() method is used to assign the object attribute its value. ... Using setattr() we can create or update attributes of a...
Read more >| notebook.community
Help on class Series in module pandas.core.series: class Series(pandas.core.base. ... StringMixin | builtins.object | | Methods defined here: | | __add__ ...
Read more >What's New — pandas 0.16.2 documentation
Documentation on how to use numba with pandas, see here. What's new in v0.16.2. New features. Pipe; Other Enhancements. API Changes; Performance Improvements ......
Read more >Using setattr() in python - Stack Overflow
My problem arose trying to use one class method/function to return data that is then put in another method/function. Perhaps a simpler approach...
Read more >pandas.core.frame — Lux 0.1.2 documentation
DataFrame.join : Similar method using indices. Notes ----- Support for specifying index levels as the `on`, `left_on`, and `right_on` parameters was added ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Overall I feel -1 on the idea of a generic
setattr
method. I would rather haveset_axis
(or any analogous method) to operate onself
then have asetattr
method able to modify anything on the frame.Closing. I don’t feel there is support for this.