Repository: dask/dask

Issue:
Read_sql_table ufunc error
Whilst trying read_sql_table I get a somewhat obscure numpy error:

    
    import dask.dataframe as dd
    from dask.distributed import Client
    client = Client()
    connection_string = "mysql+pymysql://{user}:{password}@{host}:{port}/{database}".format(user=user, password=password, host=host, database=database, port=port)
    dd.read_sql_table(given_table_name, connection_string, index_col = 'idTx')

The error I am getting is the following:

```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-a060bf974152> in <module>()
----> 1 dd.read_sql_table('Logger_init', connection_string, index_col = 'idTx')

~/miniconda3/lib/python3.6/site-packages/dask/dataframe/io/sql.py in read_sql_table(table, uri, index_col, divisions, npartitions, limits, columns, bytes_per_chunk, head_rows, schema, meta, engine_kwargs, **kwargs)
    153             divisions[-1] = maxi
    154         else:
--> 155             divisions = np.linspace(mini, maxi, npartitions + 1).tolist()
    156 
    157     parts = []

~/miniconda3/lib/python3.6/site-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype)
    106     # Convert float/complex array scalars to float, gh-3504
    107     # and make sure one can use variables that have an __array_interface__, gh-6634
--> 108     start = asanyarray(start) * 1.0
    109     stop  = asanyarray(stop)  * 1.0
    110 

TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
```
I tried several index cols and got the same error. If I select a date column as index I get this different error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-1a4e483e85a4> in <module>()
----> 1 dd.read_sql_table('Logger_init', connection_string, index_col = 'date')

```
~/miniconda3/lib/python3.6/site-packages/dask/dataframe/io/sql.py in read_sql_table(table, uri, index_col, divisions, npartitions, limits, columns, bytes_per_chunk, head_rows, schema, meta, engine_kwargs, **kwargs)
    149             divisions = pd.date_range(
    150                 start=mini, end=maxi, freq='%iS' % (
--> 151                     (maxi - mini) / npartitions).total_seconds()).tolist()
    152             divisions[0] = mini
    153             divisions[-1] = maxi

AttributeError: 'numpy.timedelta64' object has no attribute 'total_seconds'
```
    


Discussion:
@martindurant any thoughts on this?
It is datetime objects which have the `total_seconds` method, but single-item numpy datetime results like this should wrap the python object. Some sources suggest using `.item().total_seconds()`, would appreciate if you were to try it, @Juanlu001 .
@martindurant I think you mentioned the wrong person :) 
Sorry, was to @JuanPabloMF

Resolving patch:
diff --git a/dask/dataframe/io/sql.py b/dask/dataframe/io/sql.py
--- a/dask/dataframe/io/sql.py
+++ b/dask/dataframe/io/sql.py
@@ -142,6 +142,7 @@ def read_sql_table(table, uri, index_col, divisions=None, npartitions=None,
         else:
             mini, maxi = limits
             dtype = pd.Series(limits).dtype
+
         if npartitions is None:
             q = sql.select([sql.func.count(index)]).select_from(table)
             count = pd.read_sql(q, engine)['count_1'][0]
@@ -149,11 +150,14 @@ def read_sql_table(table, uri, index_col, divisions=None, npartitions=None,
         if dtype.kind == "M":
             divisions = pd.date_range(
                 start=mini, end=maxi, freq='%iS' % (
-                    (maxi - mini) / npartitions).total_seconds()).tolist()
+                    (maxi - mini).total_seconds() / npartitions)).tolist()
             divisions[0] = mini
             divisions[-1] = maxi
-        else:
+        elif dtype.kind in ['i', 'u', 'f']:
             divisions = np.linspace(mini, maxi, npartitions + 1).tolist()
+        else:
+            raise TypeError('Provided index column is of type "{}".  If divisions is not provided the '
+                            'index column type must be numeric or datetime.'.format(dtype))
 
     parts = []
     lowers, uppers = divisions[:-1], divisions[1:]


Test patch:
diff --git a/dask/dataframe/io/tests/test_sql.py b/dask/dataframe/io/tests/test_sql.py
--- a/dask/dataframe/io/tests/test_sql.py
+++ b/dask/dataframe/io/tests/test_sql.py
@@ -257,3 +257,10 @@ def test_extra_connection_engine_keywords(capsys, db):
     out, err = capsys.readouterr()
     assert "SELECT" in out
     assert_eq(data, df)
+
+
+def test_no_character_index_without_divisions(db):
+
+    # attempt to read the sql table with a character index and no divisions
+    with pytest.raises(TypeError):
+        read_sql_table('test', db, npartitions=2, index_col='name', divisions=None)
diff --git a/test b/test
new file mode 100644
Binary files /dev/null and b/test differ