-
Notifications
You must be signed in to change notification settings - Fork 117
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
[Bug]: pipe()
can not pass column values to scipy.stats.t.cdf
to compute p-value for example
#1225
Comments
Maybe the solution would be in this example on github |
Using polars we could do something similar to: df.with_columns(
pl.col("statistic")
.map_elements(
lambda x: (2 * (1 - stats.t.cdf(np.abs(x), dof))), return_dtype=pl.Float64
)
.alias("p_value")
) The above is my try to converting this function to use narwhals. |
thanks @artiom-matvei for the report I think what you're trying to do in Polars is import pandas as pd
import polars as pl
import narwhals as nw
import numpy as np
from scipy.stats import t
df = {
"statistic": [1, 2, -3, 4],
}
df_pd = pd.DataFrame(df)
df_pl = pl.DataFrame(df)
def p_value(df):
dof = 4
return df.with_columns(
pl.col("statistic")
.map_batches(
lambda x: (2 * (1 - t.cdf(np.abs(x), dof))), return_dtype=pl.Float64
)
.alias("p_value")
)
print(p_value(df_pl)) right? If so, we don't (yet) support In the meantime, if import pandas as pd
import polars as pl
import narwhals as nw
import numpy as np
from scipy.stats import t
import narwhals as nw
df = {
"statistic": [1, 2, -3, 4],
}
df_pd = pd.DataFrame(df)
df_pl = pl.DataFrame(df)
@nw.narwhalify
def p_value(df):
dof = 4
return df.with_columns(
nw.new_series(
"p_value",
2 * (1 - t.cdf(np.abs(df["statistic"]), dof)),
dtype=nw.Float64,
native_namespace=nw.get_native_namespace(df),
)
)
print(p_value(df_pl)) Once we address #1226, then you should be able to use |
closing in favour of #1226 as that'll address this, thanks again for the issue! |
Describe the bug
It seems like there is a problem in how narwhals passes values to lambda functions.
Steps or code to reproduce the bug
This is a stub that eventually will be used to compute a p-value. I removed what is not necessary to reproduce the bug.
Expected results
I expect this to return some floats that will be used to compute the p-value.
Actual results
Basically I get a
TypeError
fromscipy
telling that something is wrong with what has been passed to it.TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Below is the stack trace:
Please run narwhals.show_version() and enter the output below.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: