yhat / pandasql

sqldf for pandas
MIT License
1.31k stars 184 forks source link

support of math functions? #67

Open SamanthaChen opened 5 years ago

SamanthaChen commented 5 years ago

I am trying to use function 'exp' in pandassql, but the error throwed:

PandaSQLException: (sqlite3.OperationalError) no such function: exp [SQL: 'select *, 1/(1+exp(-(tree0+tree1+tree2+tree3+tree4+tree5+tree6+tree7+tree8+tree9))) as score from res_df;']

yumok commented 5 years ago

check sqlite3 documentation, as pandasql wrapps your dfs, "sends" them to sqlite3, executes your sql query in sqlite3, and returns results as pandas_dataframe

stonebig commented 4 years ago

as for https://github.com/yhat/pandasql/issues/78

EXP is not in standard SQLite.

It's available in a C extension of SQLite "extension-functions.c" described here https://www.sqlite.org/contrib?orderby=date

The possible workaround is you to inject the needed function in Python in SQLite database.

It would do something like that, in a nasty proof of concept.

image

ossings commented 3 years ago

as for #78

EXP is not in standard SQLite.

It's available in a C extension of SQLite "extension-functions.c" described here https://www.sqlite.org/contrib?orderby=date

The possible workaround is you to inject the needed function in Python in SQLite database.

It would do something like that, in a nasty proof of concept.

image

I have tried your solution, but I get:

DatabaseError: Execution failed on sql 'pydef py_sqrt(s):
    "sqrt function from Python Math standard library"
    import math as py_math
    return ("%s" % py_math.sqrt(float(s)));': near "pydef": syntax error

for the statement:

st = """
pydef py_sqrt(s):
    "sqrt function from Python Math standard library"
    import math as py_math
    return ("%s" % py_math.sqrt(float(s)));
select r.* [...]

Am I missing something? Also included the baresql bits from your snippet.

stonebig commented 3 years ago

On all latest python releases of early may, math functions are native in SQLite-0.35.5 embedded in Python. So you won't need my trick, and worst case you can copy the sqlite3.dll from a new python into your more full size existing environement

stonebig commented 3 years ago

https://sqlite.org/releaselog/3_35_5.html

mckeown12 commented 11 months ago

@stonebig I'm running pandasql==0.7.3 in python 3.11.3 with sqlite3.version==2.6.0 and sqlite3.sqlite_version==3.37.0 but math functions still are not working.

I've been able to compile sqlite with the -DSQLITE_ENABLE_MATH_FUNCTIONS flag, but am unsure how to make the python libraries use my version. Has anyone had success here?