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

summarize() can't use np.sqrt #399

Closed
HuangHam opened this issue Feb 10, 2022 · 3 comments
Closed

summarize() can't use np.sqrt #399

HuangHam opened this issue Feb 10, 2022 · 3 comments

Comments

@HuangHam
Copy link

Hi! So glad to find a tidyverse equivalent in python. I encountered the following issue:

data = pd.concat([df_human, df_sim]) >>
group_by(_.subj, _.trial, .split, .agent,.inequality) >>
summarize(reward = np.sqrt(np.mean(
.reward)))

Note I wanted the square root of the mean of the variable named reward. but this gives me an error: invalid array_struct . This error doesn't show up for other np functions such as np.size, np.mean, np.std. So I'm really confused...

@machow machow added this to siuba Mar 22, 2022
@machow
Copy link
Owner

machow commented Mar 29, 2022

Hey--

  • siuba uses the _ to represent lazy expressions on data, rather than the data itself.
  • you can use pandas methods with it. E.g. _.x.mean()
  • you can replace calls like
    • bad: np.sqrt(some_pandas_series)
    • good: some_pandas_series.pipe(np.sqrt)
    • siuba: _.some_pandas_series.pipe(np.sqrt)
from pandas import Series
import numpy as np

ser = Series([1,2,3])

# doesn't work when translated to siuba
np.sqrt(ser)

# use this
ser.pipe(np.sqrt)

@machow
Copy link
Owner

machow commented Mar 29, 2022

I'll work on supporting calls like np.sqrt(_.some_col) using numpy's dispatch mechanisms. (But it might not be possible).

@machow
Copy link
Owner

machow commented Apr 30, 2022

Fixed in version 0.2.3!

from siuba.data import mtcars
from siuba import _, mutate, group_by
import numpy as np

mtcars >> group_by(_.cyl) >> mutate(res = np.sqrt(np.mean(_.hp)))

@machow machow closed this as completed Apr 30, 2022
@machow machow moved this to Done in siuba Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants