【问题标题】:timestamp error while importing datat from csv to postgresql将数据从 csv 导入 postgresql 时出现时间戳错误
【发布时间】:2021-01-08 14:25:28
【问题描述】:
import uuid
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://postgres:xxx@localhost:5432/xxxxa')

df=pd.read_csv("uplauds_corp_financial.csv")
li=[]
for _ in range(len(df)):
    ud = uuid.uuid1()
    print(ud)
    li.append(ud)
df['uuid_']=li
df.to_sql(name="uplauds_corp_financial", con=engine, if_exists='append', index=False)

ERROR=sqlalchemy.exc.DataError: (psycopg2.errors.InvalidDatetimeFormat) 类型时间戳无效的输入语法:“23:37.2” 第 1 行:...75、140.89、75.92、0.0、0.0、0.0、0.0、0.0、NULL、'23:37.2'、...

也许它把它当作字符串也许这就是它显示错误的原因
是否有任何选项可以代替在我的 csv 中更改它 我可以在导入 postgresql 之前更改它

enter image description here

【问题讨论】:

  • 它试图将时间解释为时间戳,但缺少秒或小时。格式应为 HH:MM:SS.ms
  • @gtomer 任何方式我都可以编辑它?除了在我的 csv 中手动执行
  • 请张贴您的部分 CSV,以便我能够提供帮助
  • @gtomer 我只能发布这部分吗?因为主 csv 有 196 列,只有 2 行
  • @gtomer 是图片可见我是新的对不起

标签: python pandas postgresql csv sqlalchemy


【解决方案1】:

错误告诉你问题所在:

select '23:37.2'::timestamp;
ERROR:  invalid input syntax for type timestamp: "23:37.2"
LINE 1: select '23:37.2'::timestamp;

#Reformatting won't help

select '23:37:00.2'::timestamp;
ERROR:  invalid input syntax for type timestamp: "23:37:00.2"
LINE 1: select '23:37:00.2'::timestamp;


#Use time type

select '23:37.2'::time;
    time    
------------
 00:23:37.2


您的数据没有提供足够的信息来用作时间戳。为了更清楚一点,如果您只想提供时间,则需要更改表字段的类型。或者,如果您将其保留为时间戳,那么您需要创建一个完整的时间戳(日期和时间)。

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2012-09-23
    相关资源
    最近更新 更多