yhat / pandasql

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

update gives no such table error #83

Open SteveScott opened 4 years ago

SteveScott commented 4 years ago

I try the following code: daily_df = pd.DataFrame({'date': [1, 2, 3, 4, 5], 'visitor_count': [10, 20, 30, 40, 50] }) row_df = pd.DataFrame({'date': [1, 2, 3], 'visitor_count': [60, 70, 80]}) column_name = 'visitor_count' test = sqldf("SELECT * FROM daily_df") sqldf("UPDATE daily_df SET {} = row_df.{} WHERE date = row_df.date;".format(column_name, column_name), globals())

The select statement works. the update statement fails and says there is no such table. If you can read a dataframe as a table, shouldn't you be able to write to a dataframe as if it were a table?

TheGlobalist commented 3 years ago

As far as I've seen in their source code, there is no support at all for UPDATE or DELETE actions.

def extract_table_names(query):
    """ Extract table names from an SQL query. """
    # a good old fashioned regex. turns out this worked better than actually parsing the code
    tables_blocks = re.findall(r'(?:FROM|JOIN)\s+(\w+(?:\s*,\s*\w+)*)', query, re.IGNORECASE)
    tables = [tbl
              for block in tables_blocks
              for tbl in re.findall(r'\w+', block)]
    return set(tables)

The regex is based only on a SELECT clause, so UPDATEs and DELETEs will fail

TheGlobalist commented 3 years ago

@SteveScott if you need it, I've made a PR for this https://github.com/yhat/pandasql/pull/85