У меня проблемы с отправкой данных на postgressql. У меня есть pandas датафрейм. Вот мой фрейм данных:
import pandas as pd
from psycopg2.extras import execute_batch
df = pd.DataFrame(
{
"portfolio_name": {
0: "Growth Portfolio",
1: "Moderate Portfolio",
2: "Conservative Portfolio",
3: "Income Portfolio",
4: "Stocks/Bonds (60/40)",
},
"portfolio_date": {
0: datetime.date(2019, 12, 3),
1: datetime.date(2019, 12, 3),
2: datetime.date(2019, 12, 3),
3: datetime.date(2019, 12, 3),
4: datetime.date(2019, 12, 3),
},
"issuer_name": {0: None, 1: None, 2: None, 3: None, 4: None},
"size_type": {0: "percent", 1: "percent", 2: "percent", 3: "percent", 4: "percent"},
"portfolio_uuid": {
0: "2dca35a7-54b3-7c6f-df32-93b71fe6b918",
1: "fdaebb1b-9a32-ae4b-4288-892038fa8248",
2: "5ad447f5-1192-ac81-cbe3-896c7632950f",
3: "25bdb447-df60-be7a-1322-ea25759aa334",
4: "e58727fc-47f6-3f75-1181-6eed6d69ab8f",
},
"ts": {
0: Timestamp("2019-12-05 01:14:12.739645"),
1: Timestamp("2019-12-05 01:14:12.739645"),
2: Timestamp("2019-12-05 01:14:12.739645"),
3: Timestamp("2019-12-05 01:14:12.739645"),
4: Timestamp("2019-12-05 01:14:12.739645"),
},
"l1": {0: "Core", 1: "Core", 2: "Core", 3: "Core", 4: "Core"},
"l2": {0: "Risk-Based", 1: "Risk-Based", 2: "Risk-Based", 3: "Risk-Based", 4: "Risk-Based"},
"l3": {
0: "Aggressive",
1: "Moderately Aggressive",
2: "Conservative",
3: "Conservative",
4: "Conservative",
},
"l4": {0: None, 1: None, 2: None, 3: None, 4: None},
}
)
# writing to database
table_name = "etfl_public_portfolio_parent"
columns = ",".join(df.columns.to_list())
values = "VALUES({})".format(",".join(["%s" for _ in range(df.size)]))
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = "INSERT INTO {} ({}) {}".format(table_name, columns, values)
curr = conn.cursor()
execute_batch(curr, insert_stmt, df.values)
conn.commit()
curr.close()
Я получаю сообщение об ошибке "Индекс 10 выходит за пределы для оси 0 с размером 10" Я не уверен, как решить эту проблему. Я попытался сначала создать тест, в котором я просто пишу первую строку кадра данных. Но это тоже не работает.