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

feat: symbolic support numpy ufuncs #415

Merged
merged 4 commits into from Apr 30, 2022
Merged

feat: symbolic support numpy ufuncs #415

merged 4 commits into from Apr 30, 2022

Conversation

machow
Copy link
Owner

@machow machow commented Apr 7, 2022

Addresses #399, by adding a dispatcher named array_ufunc, that is used in Symbolic.__array_ufunc__.

TODO:

  • document (edit: punting on this, will set aside a day for misc documentation tasks)
  • test array_ufunc concretes
  • verify that it will fail with SQL (but that defining a concrete method allows it to work!)

Example:

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

res1 = mtcars >> mutate(res = _.hp - np.mean(_.hp))
res2 = mtcars >> mutate(res = _.hp - _.hp.mean())

# note that they are only equal because there are no missing values
res1.equals(res2)      # True

Here's what the symbolic looks like

np.sqrt(_)
█─'__call__'
├─█─'__custom_func__'
│ └─<function array_ufunc at 0x106959310>
├─_
├─<ufunc 'sqrt'>
├─'__call__'
└─_

@machow
Copy link
Owner Author

machow commented Apr 22, 2022

Quick note--it seems like np.sum is not itself a ufunc....

from siuba import _
import numpy as np

np.sum(_)
█─'__call__'
├─█─.
│ ├─_
│ └─'sum'
├─axis = None
└─out = None

edit:

This also breaks with polars, and seems to be more of a numpy issue:

import polars as pl

df = pl.read_csv("https://j.mp/iriscsv")

# okay
np.sqrt(df.sepal_length)
# error
np.sum(df.sepal_length)

@machow
Copy link
Owner Author

machow commented Apr 30, 2022

See numpy/numpy#21387 for added context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant