-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: Apply function on a column easily, maintaining fluent interface #38229
Comments
-1 on yet another form of apply the .assign interface is very flexible, concise, readable and works well |
Have you considered:
|
I meant the example as an illustration of how doing an operation on a single column of a dataframe breaks the method chaining, (unless one uses .assign and a lambda). I would like to be able to write e.g. ...
return (
pd.DataFrame([["05SEP2014", 3]], columns=["date", "other_col"])
.apply_to("date", element_wise_date_parser)
.query("2 > other_col > 4")
.to_json()
) And not ...
df = pd.DataFrame([["05SEP2014", 3]], columns=["date", "other_col"])
df["date"] = fix_date(df["date"]
return df.query("2 > other_col > 4").to_json() But I guess I'll have to settle for .assign and lambda as @jreback objects, like so: ...
return (
pd.DataFrame([["05SEP2014", 3]], columns=["date", "other_col"])
.assign(date=lambda x: x["date"].apply(element_wise_date_parser))
.query("2 > other_col > 4")
.to_json()
) |
similar to #40322 |
Thanks for the suggestion, but it appears that there isn't much appetite for supporting this feature in pandas. Closing but happy to reopen if there is a changed view for this feature. |
I really like the pandas fluent / method chaining interface but it is not always convenient to use. I often end up writing code like so:
This is ok, but I would much rather write this in a fluent style. As far as I am aware this is only possible with
.assign
and.pipe
, and would give something like so:I find that syntax not so easy to read and write. And as I find myself needing to perform an operation on a single column of a dataframe quite often, I would like to have a better way to do that. I propose add an apply_to method for the element wise which I made an example monkeypatch implementation:
With these examples I find it much is easier to in-place modify a column without breaking the fluent interface. Of course adding even more methods to the already broad dataframe API is not free so am not 100% sure this is a good idea. But I wanted to put it up here anyway as often see myself and others cluttering code with unnecessary intermediate dataframes and or repeatedly reassigning
df
due to not knowing how to keep the fluent interface. And sprinkling around "assign with lambdas" everywhere is also not that appealingThe text was updated successfully, but these errors were encountered: