我有以下(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:

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

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.

Is there a way to troubleshoot this and understand why this is occurring? I'm very confused as to what's going on here.
发布评论
评论(2)
考虑要么考虑将您的日期值转换为 (不是
对象
)或dateTime64
没有dt.date
属性。 MySQL可能会将Python DateTimes映射到其date
type(即减去时间组件)。还考虑执行人
来自dataframe.to_numpy()
。请注意:
astype(“ String”)
呈现StringDtype
,并且与astype(str)(str)
或astype(“ str”(“ str””)不同)
渲染对象
类型。Consider either converting your date values to
StringDtype
(notobject
) or todatetime64
without thedt.date
attribute. MySQL may map Python datetimes to itsdate
type (i.e., minus time component). Consider alsoexecutemany
fromDataFrame.to_numpy()
.Do note:
astype("string")
renders theStringDtype
and is different thanastype(str)
orastype("str")
which renderobject
type.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.