日期未从Python正确导入SQL数据库

发布于 2025-01-30 15:17:44 字数 1370 浏览 4 评论 0 原文

我有以下(a)pandas dataframe的(子集),我正在尝试上传到mySQL数据库:

这是使用info()函数的数据详细信息:

“在此处输入图像说明”

对于添加的上下文,我先前已将 tourn_date 转换过日期使用此代码到DateTime格式的列:

pd.to_datetime(all_data['tourn_date']).dt.date

最后,这是我用来将数据导入MySQL数据库的代码:

for index, row in all_data.iterrows():
    inserts = [row.tourn_id, row.year, row.round_num, row.tourn_date, row.date]

    cursor.execute("""INSERT INTO AllPlayerStats 
                    (TourneyID, Year, RoundNum, TourneyDate, TDate)
                    values(%s,%s,%s,%s,%s)""", inserts)
db.commit()
cursor.close()

但是,当我运行此期间时,一些日期(如所示的日期)上面的),尽管其他锦标赛正常运行,并且 tourneydate date date 列是数据库中的列,但在数据库中以null的形式出现(见下文)。 。在某些情况下, tourn_date 正确上传,但 date 却没有。

有没有办法解决此问题并了解为什么会发生这种情况?我对这里发生的事情感到非常困惑。

I have the following (subset of a) Pandas dataframe that I am trying to upload to a MySQL database:
enter image description here

Here are details of the data using the info() function:

enter image description here

For added context, I previously converted both the tourn_date and date column to a datetime format using this code:

pd.to_datetime(all_data['tourn_date']).dt.date

Finally, here is the code that I'm using to import the data into the MySQL database:

for index, row in all_data.iterrows():
    inserts = [row.tourn_id, row.year, row.round_num, row.tourn_date, row.date]

    cursor.execute("""INSERT INTO AllPlayerStats 
                    (TourneyID, Year, RoundNum, TourneyDate, TDate)
                    values(%s,%s,%s,%s,%s)""", inserts)
db.commit()
cursor.close()

However, when I run this, some of the dates (like the ones shown above), are appearing as NULL in the database (see below) despite other tournaments working just fine and the format of the TourneyDate and Date columns in the database being of date type. In some instances, the tourn_date uploads correctly but the date doesn't.

enter image description here

Is there a way to troubleshoot this and understand why this is occurring? I'm very confused as to what's going on here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赴月观长安 2025-02-06 15:17:44

考虑要么考虑将您的日期值转换为 (不是对象)或 dateTime64 没有 dt.date 属性。 MySQL可能会将Python DateTimes映射到其 date type(即减去时间组件)。还考虑执行人来自 dataframe.to_numpy()

请注意: astype(“ String”)呈现 StringDtype ,并且与 astype(str)(str) astype(“ str”(“ str””)不同)渲染对象类型。

sql = """INSERT INTO AllPlayerStats (TourneyID, Year, RoundNum, TourneyDate, TDate)
         VALUES (%s, %s, %s, %s, %s)"""

# StringDtype
all_data["tourn_date"], all_data["date"] = (
    all_data["tourn_date"].astype("string"), 
    all_data["date"].astype("string")
)

# datetime
all_data["tourn_date"], all_data["date"] = (
    pd.to_datetime(all_data["tourn_date"]), 
    pd.to_datetime(all_data["date"])
)

vals = all_data[["tourn_id", "year", "round_num", "tourn_date", "date"]].to_numpy()

cursor.executemany(sql, vals) 
db.commit() 
cursor.close()

Consider either converting your date values to StringDtype (not object) or to datetime64 without the dt.date attribute. MySQL may map Python datetimes to its date type (i.e., minus time component). Consider also executemany from DataFrame.to_numpy().

Do note: astype("string") renders the StringDtype and is different than astype(str) or astype("str") which render object type.

sql = """INSERT INTO AllPlayerStats (TourneyID, Year, RoundNum, TourneyDate, TDate)
         VALUES (%s, %s, %s, %s, %s)"""

# StringDtype
all_data["tourn_date"], all_data["date"] = (
    all_data["tourn_date"].astype("string"), 
    all_data["date"].astype("string")
)

# datetime
all_data["tourn_date"], all_data["date"] = (
    pd.to_datetime(all_data["tourn_date"]), 
    pd.to_datetime(all_data["date"])
)

vals = all_data[["tourn_id", "year", "round_num", "tourn_date", "date"]].to_numpy()

cursor.executemany(sql, vals) 
db.commit() 
cursor.close()
鯉魚旗 2025-02-06 15:17:44

Python声称日期是数据类型对象,您将需要首先将它们作为日期类型施放。有关如何在此处找到的日期的信息:

另外,请确保您插入的MySQL列也具有正确的数据类型格式。大多数没有任何意义的问题通常是数据类型问题,都太容易将x = 1与y =“ 1”进行比较,并且不明白为什么x不= y = y。

更新 - 出现第二个答案,概念是相同的,我想区别在于您想与Python或MySQL一起做。

Python is claiming the dates are datatype Object, you will need them to be Cast as a Date type first. Information about how to Cast as a Date found here:

https://www.w3schools.com/sql/func_mysql_cast.asp

Also, make sure that the MySQL column you are inserting into has the correct data type format as well. Most problems that make no sense are usually data type issues, all too easy to compare x = 1 to y = "1" and not understand why x is NOT = to y for example.

UPDATE - a second answer appeared, the concepts are the same, I guess the difference is if you want to do it with Python or with MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文