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

ENH: function for truncating (deleting all rows) an existing table #8673

Closed
adieldar opened this issue Oct 29, 2014 · 4 comments
Closed

ENH: function for truncating (deleting all rows) an existing table #8673

adieldar opened this issue Oct 29, 2014 · 4 comments
Labels
IO SQL to_sql, read_sql, read_sql_query

Comments

@adieldar
Copy link

Hi,
I want to truncate (or even drop) an existing table in SQL server. I didn't find any dedicated function in pandas.io.sql. So I tried to create an empty data frame just to call:

df1 = pd.DataFrame()
df1.to_sql(out_tbl, engine, if_exists='replace', index=False)

This code does drop the table but then generates exception from sqlalchemy as it tries to re-create an empty table. I can catch and ignore it, and the next call to_sql() with if_exists='append' will create the table correctly, but this is pretty ugly.

Another way I tried is to clear all the rows from the original data frame, leaving the columns:

df1 = df.drop(df.index)
df1.to_sql(out_tbl, engine, if_exists='replace', index=False)

This almost works: it truncates the table but then insert a single record with all fields as NULL...

You can see the full description at http://stackoverflow.com/questions/26205057/how-can-i-truncate-a-table-using-pandas#comment41130846_26205057

@jorisvandenbossche jorisvandenbossche added the IO SQL to_sql, read_sql, read_sql_query label Oct 29, 2014
@jorisvandenbossche
Copy link
Member

For dropping a table, you can:

  • use the sqlalchemy api:

    meta = sqlalchemy.MetaData()
    meta.reflect(bind=engine)
    table = meta.tables['table_name']
    table.drop(engine)
    
  • or with pandas

    pdsql = pd.io.sql.SQLDatabase(engine)
    pdsql.drop_table('table_name')
    

If you want to delete all rows (but not drop the table), that is currently not possible with pandas, but I think delete does this in sqlalchemy:

meta = sqlalchemy.MetaData()
meta.reflect(bind=engine)
table = meta.tables['table_name']
table.delete(engine)

But maybe we should think about how we could integrate this in the pandas api.

@adieldar
Copy link
Author

Thanks for the quick and detailed response!
Indeed in this case I want to truncate it (i.e. delete all rows and leave it empty), and internally I'm sure it's already implemented as to_sql supports if_exists='replace', so I think that either use to_sql with empty df or explicit API to truncate it could be useful
Adi

@jorisvandenbossche jorisvandenbossche changed the title BUG: Truncating an existing table ENH: function for truncating (deleting all rows) an existing table Oct 30, 2014
@lam-juice
Copy link

Is it possible to make pandas delete all the rows instead of dropping the table for if_exists='replace'?
Having to drop the table results in deadlocks when there's a simultaneous SELECT going on, while a simple deletion would avoid such deadlocks.

@wesm wesm added the Won't Fix label Jul 6, 2018
@wesm
Copy link
Member

wesm commented Jul 6, 2018

Closing for now as Won't Fix. PRs welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

No branches or pull requests

4 participants