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

Add support for the . operator (and related work) #216

Merged
merged 6 commits into from
Dec 2, 2024

Conversation

matthewwardrop
Copy link
Owner

@matthewwardrop matthewwardrop commented Nov 27, 2024

This is a relatively invasive and large patch set that adds support for the . operator in formulae (and potentially in the future support for other types of operator dynamism driven by execution context). Since the . operator depends on the data context, this weakens the strict separation of parsing and materialization, but this patch retains this distinction and simply requires users to pass in the context twice if using the lower level API; or does it for the user if they are interacting with formulaic using syntactic sugar methods like model_matrix. If you are not using operator dynamism, then you can completely ignore this functionality and keep the clean separation as in times of yore.

Example usage:

import pandas
from formulaic import model_matrix
df = pandas.DataFrame({"x": [1,2,3], "y": [4,5,6], "z": [7,8,9]})

model_matrix("y ~ .", df)

outputs:

.lhs:
       y
    0  4
    1  5
    2  6
.rhs:
       Intercept  z  x
    0        1.0  7  1
    1        1.0  8  2
    2        1.0  9  3

More complicated usage is also supported:

model_matrix("y ~ .^2 - z", df)

outputs:

.lhs:
       y
    0  4
    1  5
    2  6
.rhs:
       Intercept  x  z:x
    0        1.0  1    7
    1        1.0  2   16
    2        1.0  3   27

closes: #175
related: #177

@matthewwardrop matthewwardrop force-pushed the add_support_for_dot_operator branch from d6c3acc to eaf9e12 Compare December 2, 2024 08:10
@matthewwardrop matthewwardrop marked this pull request as ready for review December 2, 2024 08:10
@matthewwardrop matthewwardrop force-pushed the add_support_for_dot_operator branch from eaf9e12 to 0d93416 Compare December 2, 2024 08:13
@matthewwardrop matthewwardrop force-pushed the add_support_for_dot_operator branch 2 times, most recently from 9d7a83f to d6c3e4c Compare December 2, 2024 08:26
@matthewwardrop matthewwardrop force-pushed the add_support_for_dot_operator branch from d6c3e4c to 8183ba8 Compare December 2, 2024 08:29
@matthewwardrop matthewwardrop merged commit 28fd9ec into main Dec 2, 2024
7 checks passed
@matthewwardrop matthewwardrop deleted the add_support_for_dot_operator branch December 2, 2024 08:33
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.

Add . operator
1 participant