d6t / d6tstack

Quickly ingest messy CSV and XLS files. Export to clean pandas, SQL, parquet
MIT License
195 stars 46 forks source link

Error while inserting a list/array object existing in a Dataframe Column to a Postgresql table column #29

Open vaibhav90 opened 3 years ago

vaibhav90 commented 3 years ago

Proving an example to replicate this issue: Consider the following Pandas Dataframe called my_data: | id | coordinates | | 01| [50.1, 68.2] | |02| [52.2, 67.9] |

and consider the following postgresql table definition

CREATE TABLE coordinates ( id int2, coordinates int8range );

Now, even if you cast this dataframe column to the type object my_data['coordinates'] = my_data['coordinates'].astype(object)

the following insert operation throws an error: d6tstack.utils.pd_to_psql(my_table, db, table, schema if_exists='replace')

The Error log is: BadCopyFileFormat: extra data after last expected column

The same insert operation works with dataframe.to_sql functionality provided by psycopg2

marksparrish commented 2 years ago

You need to add sep='\t' to the call.

d6tstack.utils.pd_to_psql(df, str(engine.url), table_name, if_exists='append',sep='\t')