Repository: dask/dask

Issue:
DataFrame.__setitem__ fails for index
```python
In [10]: import pandas as pd

In [11]: import dask.dataframe as dd

In [12]: df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})

In [13]: ddf = dd.from_pandas(df, 2)

In [14]: df[df.columns] = 1

In [15]: ddf[ddf.columns] = 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-736f0f46dec1> in <module>()
----> 1 ddf[ddf.columns] = 1

~/sandbox/dask/dask/dataframe/core.py in __setitem__(self, key, value)
   2430                                 for k, c in zip(key, value.columns)})
   2431         else:
-> 2432             df = self.assign(**{key: value})
   2433
   2434         self.dask = df.dask

~/Envs/dask-dev/lib/python3.6/site-packages/pandas/core/indexes/base.py in __hash__(self)
   2060
   2061     def __hash__(self):
-> 2062         raise TypeError("unhashable type: %r" % type(self).__name__)
   2063
   2064     def __setitem__(self, key, value):

TypeError: unhashable type: 'Index'
```

pandas index objects aren't hashable, putting them in a dictionary fails. We should probably special Index objects in `DataFrame.__setitem__` and convert them to lists.

Discussion:

Patch:
diff --git a/dask/array/creation.py b/dask/array/creation.py
index 8884a06c7..c35c191f2 100644
--- a/dask/array/creation.py
+++ b/dask/array/creation.py
@@ -905,7 +905,7 @@ def pad_stats(array, pad_width, mode, *args):
 
 
 def wrapped_pad_func(array, pad_func, iaxis_pad_width, iaxis, pad_func_kwargs):
-    result = array.copy()
+    result = np.empty_like(array)
     for i in np.ndindex(array.shape[:iaxis] + array.shape[iaxis + 1:]):
         i = i[:iaxis] + (slice(None),) + i[iaxis:]
         result[i] = pad_func(array[i], iaxis_pad_width, iaxis, pad_func_kwargs)
diff --git a/dask/dataframe/core.py b/dask/dataframe/core.py
index 1557791bd..6959539a3 100644
--- a/dask/dataframe/core.py
+++ b/dask/dataframe/core.py
@@ -2490,9 +2490,13 @@ class DataFrame(_Frame):
         raise NotImplementedError(key)
 
     def __setitem__(self, key, value):
-        if isinstance(key, (tuple, list)):
+        if isinstance(key, (tuple, list)) and isinstance(value, DataFrame):
             df = self.assign(**{k: value[c]
                                 for k, c in zip(key, value.columns)})
+
+        elif isinstance(key, pd.Index) and not isinstance(value, DataFrame):
+            key = list(key)
+            df = self.assign(**{k: value for k in key})
         else:
             df = self.assign(**{key: value})
 


Test patch:
diff --git a/dask/dataframe/tests/test_dataframe.py b/dask/dataframe/tests/test_dataframe.py
index 58f79bf73..7a42bb91e 100644
--- a/dask/dataframe/tests/test_dataframe.py
+++ b/dask/dataframe/tests/test_dataframe.py
@@ -3320,3 +3320,11 @@ def test_partitions_indexer():
     assert ddf.x.partitions[:3].npartitions == 3
 
     assert ddf.x.partitions[::2].compute().tolist() == [0, 1, 4, 5, 8, 9]
+
+
+def test_setitem():
+    df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
+    ddf = dd.from_pandas(df.copy(), 2)
+    df[df.columns] = 1
+    ddf[ddf.columns] = 1
+    assert_eq(df, ddf)