【问题标题】:dask read_sql error when querying from MYSQL从 MYSQL 查询时出现 dask read_sql 错误
【发布时间】:2018-05-14 09:15:55
【问题描述】:

我正在使用带有 dask 的 python 2.7 并尝试从远程机器查询 db 表到 dask 数据帧

我在表中有一个多列索引,我尝试使用以下脚本读取它

ddf = dd.read_sql_table("table name", "mysql://user:pass@ip:port/Dbname",spesific column name).head()

并得到以下错误

start = asanyarray(start) * 1.0 TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32')

dtype('S32') dtype('S32')

我得到了 here 解释的 sqlalchemy uri

我不确定是什么问题,当我尝试通过另一列作为索引进行查询并且仅使用 ddf head() 时,我没有收到错误,并且当我尝试计算整个 ddf我得到同样的错误,我认为这是关于列不是唯一值的问题,我没有单列索引,而是多列,在这里读取整个表的解决方案是什么?

谢谢。

完整的回溯

> Traceback (most recent call last):   File "path", line 28, in <module>
>     ddf = dd.read_sql_table("tablename", "mysql://user:pass@ip:port/dbname","indexcolumn")   File "file", line
> 123, in read_sql_table
>     divisions = np.linspace(mini, maxi, npartitions + 1).tolist()   File
> "/home/user/.local/lib/python2.7/site-packages/numpy/core/function_base.py",
> line 108, in linspace
>     start = asanyarray(start) * 1.0 TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32')
> dtype('S32') dtype('S32')

【问题讨论】:

  • 你能验证等效的 pandas 操作是否有效吗?
  • 请显示更详细的回溯,并在错误发生时运行调试以查找start 的值。
  • @MRocklin 与熊猫配合得很好
  • @mdurant start 的值为 {str}'-1000001542'
  • 看起来你看到了一个应该是数字的字符串

标签: python mysql dataframe sqlalchemy dask


【解决方案1】:

对于您没有提供更多信息或仅指定分区数量的情况,read_sql_table 中的分区逻辑仅适用于数字,因为我们需要一种方法来在最小值和最大值之间进行有序除法。

显然,但查询(获取最大值/最小值)在这种情况下返回了一个字符串。 read_sql_table 仍然可以工作,但您需要自己定义要拆分的部门,并为它们提供部门关键字,例如,

ddf = dd.read_sql_table("table name", "mysql://user:pass@ip:port/Dbname", 
    'index_col', divisions=['aardvark', 'llama', 'tapir', 'zebra']).head()

另外,有问题的字符串肯定看起来像一个数字,因此您可能需要更新表的架构以确保它被解释为数字。

【讨论】:

  • 您能否提供第一个解决方案的完整示例?
  • 您能否提供第一个解决方案的工作示例?
  • 谢谢!所以,只是为了让我一直理解它,划分只是索引列和索引列吗? (考虑到您需要对字符串进行排序以适应这些分区,它将如何使用它们,它是否使用字典顺序?
  • 是的,分区是索引列的每个分区的边界,比如第二个分区是WHERE index_col &gt; "llama" AND index_col &lt;= "tapir"。如果您提供分区,则由您来订购它们,并了解您的数据库将如何理解它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多