python - 为什么我通过pandas访问MySQL时得到Timedelta类型

标签 python mysql pandas

MySQL中的数据类型是time,但是当我用pandas访问它时,dataframe中的类型变成了Timedelta。 我的代码是

df = pandas.read_sql_query(“select * from table1”, con=newConnect)

最佳答案

因为timedelta在Pandas/Numpy中最接近MySQL的time数据类型。

考虑以下 DataFrame:

In [306]: df
Out[306]:
       time
0  10:11:12
1  22:23:24

In [307]: df.dtypes
Out[307]:
time    object
dtype: object

您可以将其转换为 datetime(这需要日期部分):

In [308]: pd.to_datetime(df.time)
Out[308]:
0   2018-06-19 10:11:12
1   2018-06-19 22:23:24
Name: time, dtype: datetime64[ns]  # <---- note the dtype

或一个timedelta:

In [309]: pd.to_timedelta(df.time)
Out[309]:
0   10:11:12
1   22:23:24
Name: time, dtype: timedelta64[ns]  # <---- note the dtype

关于python - 为什么我通过pandas访问MySQL时得到Timedelta类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931416/

相关文章:

python - Pandas - 提取唯一的列组合并在另一个表中对它们进行计数

python - 在 python pandas 中读取缺失值的文件

python - 如何防止在 kivy 应用程序中关闭 x 上的窗口

python - 在 python 中,如果文件以一组特定的字母开头,我如何从循环中排除文件?

python - Biopython 全局对齐 : Out of Memory

mysql - 尝试从 MySQL 数据库创建 Entity Framework 模型失败并出现意外异常

python - 朱皮特 : Why does this second command stop the first one from working?

Python 和 json 问题

php - 如果在选择标签html php中选择了男性/女性,如何仅输出男性/女性学生

mysql - 级联副本仍然适用于 RDS MySQL 吗?