error inserting values to db with psycopg2 module [duplicate]

2024/10/4 5:30:28

I am attempting to insert a dataframe into my postgres database using the pscycopg2 module used with sqlalchemy. The process is loading an excel file into a pandas dataframe and then inserting the dataframe into the database via the predefined table schema.

I believe these are the relevant lines of code:

post_meta.reflect(schema="users")
df = pd.read_excel(path) 
table = sql.Table(table_name, post_meta, schema="users")
dict_items = df.to_dict(orient='records')
connection.execute(table.insert().values(dict_items))

I'm getting the following error:

<class 'sqlalchemy.exc.ProgrammingError'>, ProgrammingError("(psycopg2.ProgrammingError) can't adapt type 'numpy.int64'",)

All data field types in the dataframe are int64.

I can't seem to find a similar question or information regarding why this error is and what it means.

Any direction would be great. Thanks

Answer

Looks like you're trying to insert numpy integers, and psycopg2 doesn't know how to handle those objects. You need to convert them to normal python integers first. Maybe try calling the int() function on each value... Please provide more context with code if that fails.

https://en.xdnf.cn/q/70641.html

Related Q&A

NaN values in pivot_table index causes loss of data

Here is a simple DataFrame:> df = pd.DataFrame({a: [a1, a2, a3],b: [optional1, None, optional3],c: [c1, c2, c3],d: [1, 2, 3]}) > dfa b c d 0 a1 optional1 c1 1 1 a2 None c2…

ModuleNotFoundError in Docker

I have imported my entire project into docker, and I am getting a ModuleNotFoundErrorfrom one of the modules I have created.FROM python:3.8 WORKDIR /workspace/ COPY . /workspace/ RUN pip install pipenv…

Can I use md5 authentication with psycopg2?

After two hours of reading documentation, source code and help-threads, Im giving up. I cant get psycopg2 to authenticate with a md5-string. According to this thread I dont have to anything besides ena…

Python checking __init__ parameter

Ive been trying to figuring this out for the last few hours, and Im about to give up.How do you make sure that in python only a matching specific criteria will create the object?For example, lets say …

Minidom getElementById not working

Minidoms getElementById function is returning None for any entry I pass to it.For example, this code:l = minidom.parseString(<node id="node">Node</node>) print(l.getElementById(&q…

Optimization on piecewise linear regression

I am trying to create a piecewise linear regression to minimize the MSE(minimum square errors) then using linear regression directly. The method should be using dynamic programming to calculate the dif…

Python: Check if list of named tuples contains particular attribute value

I have a list of named tuples:from collections import namedtupleT = namedtuple(T, [attr1, attr2, attr3, attr4]) t1 = T(T1, 1, 1234, XYZ) t2 = T(T2, 2, 1254, ABC) t3 = T(T2, 2, 1264, DEF) l = [t1, t2, t…

javascript error: arguments[0].scrollIntoView is not a function using selenium on python

Im using Selenium on python and I would like to scroll to an element to click on it. Everywhere I see that the rigth things to do to go directly to the element is to use :driver = webdriver.Chrome() dr…

Uploading a static project to google app engines

Disclaimer: I already asked here, but apparently off-topic. I want to set up a page using this bootstrap template and host it as a static website using the google appengine service. Inside the google_a…

Python cannot import DataFrame

I am trying to use Pandas in Python to import and manipulate some csv file.my code is like:import pandas as pd from pandas import dataframe data_df = pd.read_csv(highfrequency2.csv) print(data_df.col…