我尝试将pandas DataFrame中的数据插入PostgreSQL表中,
尝试插入的表如下:
city_id date forecast
5 29.05.2019 0
1 29.05.2019 0
151 29.05.2019 0
55 29.05.2019 0
...
类型:
city_id
-numpy.int64
date
-datetime.date
forecast
-numpy.int64
以及将数据插入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))
其中now
被保存为datetime.datetime.now()
的时间
然后我得到 ProgrammingError :
ProgrammingError: can't adapt type 'numpy.int64'
我检查了类型,type(df_with_new_one['forecast'][0])
类型为numpy.int64
所以我发现PostreSQL只能读取pythonic int
和float
,而我尝试的第一件事是将np.int64
转换为简单的int
: >
tolist()
pd.to_numeric()
int()
for ((int(city_id), now, date, int(value))
.astype(int)
.value.astype('int')
更新。
city_id = int(df_with_new_one['city_id'][i])
value = int(df_with_new_one['forecast'][i])
不幸的是,他们都不对我有用
尝试int()
时出现另一个错误:
TypeError: cannot convert the series to <class 'int'>
我找到的答案,但没有人帮助我
是否有任何其他方法来更改值的类型?
答案 0 :(得分:0)
您可以使用numpy.ndarray.item()
type(np.arange(1)[0])
# numpy.int64
type(np.arange(1)[0].item())
# int
答案 1 :(得分:0)
首先,您的方法效率不高,因为您反复调用cursor.execute
而不是调用cursor.executemany
。
但是当您传递个人价值观时,很容易及时地将它们转换:
...
for i in df_with_new_one.index:
date = df_with_new_one['date'][i]
city_id = int(df_with_new_one['city_id'][i])
value = int(df_with_new_one['forecast'][i])
...
答案 2 :(得分:0)
问题出在错误的索引编制中:
因此,问题已通过.reset_index()
df_with_new_one.reset_index(drop = True, inplace = True)
谢谢大家的回答!