I try to insert data from pandas DataFrame into the PostgreSQL table,
table that a try to insert looks like:
city_id date forecast
5 29.05.2019 0
1 29.05.2019 0
151 29.05.2019 0
55 29.05.2019 0
...
types:
city_id
-numpy.int64
date
-datetime.date
forecast
-numpy.int64
And the block of code, that inserting data to db:
with psycopg2.connect(f"host='{hostname}' \
dbname='{database}' \
user='{username}' \
password='{password}'") as connection:
with connection.cursor() as cursor:
connection.set_client_encoding('UTF8')
for i in df_with_new_one.index:
date = df_with_new_one['date'][i]
city_id = df_with_new_one['city_id'][i]
value = df_with_new_one['forecast'][i]
cursor.execute("INSERT INTO forecast \
(city_id, computed_time, date, value) \
VALUES (%s, %s, %s, %s)", (city_id, now, date, value))
Where now
is time saved as datetime.datetime.now()
And i get ProgrammingError:
ProgrammingError: can't adapt type 'numpy.int64'
I checked type type(df_with_new_one['forecast'][0])
type is numpy.int64
So I get that PostreSQL can read only pythonic int
and float
, and the first thing i've tried was converting np.int64
into simple int
with:
tolist()
pd.to_numeric()
int()
for((int(city_id), now, date, int(value))
.astype(int)
.value.astype('int')
Upd.:
city_id = int(df_with_new_one['city_id'][i])
value = int(df_with_new_one['forecast'][i])
Unfortunately none of them works for me
When I tried int()
I get another error:
TypeError: cannot convert the series to <class 'int'>
Answers that i found, but no one of them helped me:
- psycopg2: can't adapt type 'numpy.int64'
- ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'numpy.ndarray'
- Python TypeError: cannot convert the series to <class 'int'> when trying to do math on dataframe
- Python Pandas filtering; TypeError: cannot convert the series to <class 'int'>
Are there any other methods to change type of values?