question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API: add a setattr method to NDFrame to assist with piping

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Oct 2, 2018

Overall I feel -1 on the idea of a generic setattr method. I would rather have set_axis (or any analogous method) to operate on self then have a setattr method able to modify anything on the frame.

0reactions
topper-123commented, Jun 25, 2020

Closing. I don’t feel there is support for this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found