question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: remove restrictions to numexpr to allow `where` etc.

See original GitHub issue

Is your feature request related to a problem?

the evaluation of a query is currently limited to the list _mathops while numexpr would support more, most notably a where (that would also solve other issues simple).

I do not see any reason for this restriction. In fact, simply adding the where runs (at least for my use case). Why is this restriction in place? Why can’t we enlarge it/directly pass it through to numexpr?

Describe the solution you’d like

Allow the full operator set that numexpr supports in the pd.eval

API breaking implications

Nothing

Alternatives

Using .where is an option if you can access the dataframe directly (although suboptimal). However, if your selection of data is based on passing a selection string around instead of the df (several reasons for this), the latter is not feasible.

The following doesn’t work:

import pandas as pd

data = {'a': [1, 2, 3]}                                                 

df = pd.DataFrame({'a': [1, 2, 3]})
df.eval('where(a>2, 42, 0)')

whereas in numexpr it does

numexpr.evaluate('where(a>2, 42, 0)', local_dict=data)

we expect this to return [0, 0, 42]

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
achimgaedkecommented, Jun 21, 2022

I have “solved” my problem by using

import pandas
pandas.core.computation.ops.MATHOPS = (*pandas.core.computation.ops.MATHOPS, "where")

Works out of the box with ternary operators, 🎉

df = pandas.DataFrame({"a": [2.0, 4.0, 5.0]})
pandas.eval("where(df.a > 3.0, df.a, 1)", target=df)

results in

0    1.0
1    4.0
2    5.0
Name: done, dtype: float64

Not exactly proud of this solution, but this shows that this feature request is probably done by adding the strings and writing some unit tests.

NB: arguments 2 and 3 have to be numbers - I’d love to have strings (same type should only be required only for arg 2 and 3) NB2: (df.a>3.0) * df.a + (df.a<=3) * 1 works like where(df.a > 3.0, df.a, 1)

0reactions
jonas-eschlecommented, Mar 21, 2021

Oc, sorry that was by mistake and it is not yet resolved.

@TomAugspurger, do you have an idea why this is not here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NumExpr 2.0 User Guide — numexpr 2.6.3.dev0 documentation
The numexpr package supplies routines for the fast evaluation of array expressions elementwise by using a vector-based virtual machine. Using it is simple:....
Read more >
HPC Python Programming - HPC Training Moodle
Hooks to compiled libraries to remove worst performance pitfalls. ... Annoyingly, numexpr has no facilities for slicing or offsets, etc.
Read more >
What's New - pycalphad 0.10.1.dev12+gebcfbdb4 ...
ENH : Allow passing dictionaries of PhaseRecord objects to equilibrium and calculate ( issue 361 ). FIX: Database parsing fails if some ...
Read more >
LaTeX2e unofficial reference manual (January 2022)
The placement of floats is subject to parameters, given below, that limit the number of floats that can appear at the top of...
Read more >
What's New — pandas 0.19.2 documentation
Removal of the previously deprecated modules pandas.io.data ... which allows optional copying if the requirements on dtype are satisfied (GH13209) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found