插入panda的时间戳到SQLite,不包括毫秒。

huangapple go评论63阅读模式
英文:

Insert panda's timestamps without miliseconds into SQLite

问题

I pull data from an Oracle database into a DataFrame with pd.read_sql_query. The resulting columns are of type Timestamp and contain only date+time. I then insert them with DataFrame.to_sql and sqlalchemy into an SQLite database and this step adds miliseconds when serialized to SQLite's TEXT column that I'd like to get rid of.

Is there an easy way to instruct sqlalchemy or pandas' to_sql to serialize only date+time without adding 0 milliseconds?

英文:

I pull data from an Oracle database into a DataFrame with pd.read_sql_query. The resulting columns are of type Timestamp and contain only date+time. I then insert them with DataFrame.to_sql and sqlarchemy into an SQLite database and this step adds miliseconds when serialized to SQLite's TEXT column that I'd like to get rid of.

Is there an easy way to instruct sqlalchemy or panda's to_sql to serialize only date+time without adding 0 miliseconds?

答案1

得分: 1

我已经弄清楚了!您需要使用to_sqldtype参数,并使用适当的方言类型指定每个列的格式:

from sqlalchemy.dialects.sqlite import DATETIME

dtype={
    "DATETIME_COLUMN": DATETIME(storage_format="%(year)04d/%(month)02d/%(day)02d %(hour)02d:%(minute)02d:%(second)02d")
}
data.to_sql("table_name", session.connection(), if_exists="append", index=False, dtype=dtype)

其中 session: sqlalchemy.orm.Session

https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#sqlalchemy.dialects.sqlite.DATETIME


相似的问题与相反的问题有关Pandas: 写入数据到 MySQL 时毫秒丢失 帮助我找到了正确的解决方法。

英文:

I've figured it out! You need to use to_sql's dtype parameter and specify the format for each column by using the appropriate dialect type:

from sqlalchemy.dialects.sqlite import DATETIME

dtype={
    "DATETIME_COLUMN": DATETIME(storage_format="%(year)04d/%(month)02d/%(day)02d %(hour)02d:%(minute)02d:%(second)02d")
}
data.to_sql("table_name", session.connection(), if_exists="append", index=False, dtype=dtype)

where session: sqlalchemy.orm.Session

https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#sqlalchemy.dialects.sqlite.DATETIME


A similar question with the opposite issue Pandas: milliseconds dropped when writing data to MySQL put me on the right track.

huangapple
  • 本文由 发表于 2023年6月13日 15:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定