如何将 python Int16 作为 smallint 导入 PostgreSQL?
How can I import python Int16 to PostgreSQL as smallint?
我在 python 中使用 psycopg2
将 pandas
数据框导入 PostgreSQL
数据库。数据框中几列的元素类型为 <class 'numpy.int16'>
。我是这样导入的:
df.to_sql(table_name, engine, method='multi', if_exists=if_exists_opt_loc, index=False)
写完之后,我看到数据库中的列的类型为 bigint
,而只需要一个 smallint
来存储 Int16
值。
如何进行导入以使列的类型为 smallint
?谢谢!
你也应该添加 dtype 参数:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
dtype={'datefld': sqlalchemy.DateTime(),
'intfld': sqlalchemy.types.SmallInteger(),
'strfld': sqlalchemy.types.NVARCHAR(length=255)
'floatfld': sqlalchemy.types.Float(precision=3,asdecimal=True)
'booleanfld': sqlalchemy.types.Boolean})
我在 python 中使用 psycopg2
将 pandas
数据框导入 PostgreSQL
数据库。数据框中几列的元素类型为 <class 'numpy.int16'>
。我是这样导入的:
df.to_sql(table_name, engine, method='multi', if_exists=if_exists_opt_loc, index=False)
写完之后,我看到数据库中的列的类型为 bigint
,而只需要一个 smallint
来存储 Int16
值。
如何进行导入以使列的类型为 smallint
?谢谢!
你也应该添加 dtype 参数:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
dtype={'datefld': sqlalchemy.DateTime(),
'intfld': sqlalchemy.types.SmallInteger(),
'strfld': sqlalchemy.types.NVARCHAR(length=255)
'floatfld': sqlalchemy.types.Float(precision=3,asdecimal=True)
'booleanfld': sqlalchemy.types.Boolean})