Python到mysql的'Timestamp'对象没有属性'translate' [英] Python to mysql 'Timestamp' object has no attribute 'translate'

查看:1474
本文介绍了Python到mysql的'Timestamp'对象没有属性'translate'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sqlalchemy将熊猫数据框加载到mysql表中.

I'm trying to load a pandas dataframe to a mysql table using Sqlalchemy.

我使用进行连接; engine = create_engine("mysql+pymysql://user:password@ip:port/db")

然后我就在跑步;

df.to_sql(con=engine, name='Table', if_exists='append', index=False, chunksize=10000);

我不断收到错误消息

AttributeError: 'Timestamp' object has no attribute 'translate'

当我使用旧版本并通过pymysql而不是sqlalchemy进行此操作时,效果很好

This worked fine when I used older versions and did this via pymysql, not sqlalchemy

我找不到任何在线帮助或想法.

I can't find anything online to help, any ideas.

谢谢

推荐答案

我将我的 timestamp 对象转换为ts.to_pydatetime(),然后再将其传递给光标,从而解决了属性错误.

I converted my timestamp object into ts.to_pydatetime() before passing it to the cursor and that solved the attribute error.

有关转换时间戳的不同方法,请参见以下答案: 在datetime,Timestamp和datetime64之间进行转换

See this answer for different ways of converting timestamps: Converting between datetime, Timestamp and datetime64

这篇关于Python到mysql的'Timestamp'对象没有属性'translate'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆