Repository: pandas-dev/pandas

Issue:
Pandas get_dummies validate "columns" input
#### Code Sample, a copy-pastable example if possible

import string, pandas

p_csv = pandas.read_csv(my_dir/myFile), index_col=0)

sepanames = sorted(p_csv["SEPARATOR"].unique())

for i in range(0, 14):
    print(i)
    col = p_csv.columns.get_loc("SEPARATOR") + 1 + i
    p_csv.insert(col, "SEPARATOR_" + sepanames[i].upper(), p_csv["SEPARATOR"].apply(lambda x: int(x == sepanames[i])))

p_csv.to_csv("my_dir/new_file.csv")

''' p_csv= pandas.read_csv(("/myDir/myFile.csv"), index_col=0)
    pandas.get_dummies(p_csv, prefix="SEPARATOR_", columns="SEPARATOR")
    p_csv.to_csv("/myDir/myNew.csv")'''

FILES:
https://www.amazon.de/clouddrive/share/h37d1hqtrj5SrZTvKdrs9gXltVKUgo8Is9BxL8WH7Sf
#### Problem description

This is all of my code. The quoted part is what I first tried, but after 20 minutes it ended through a sigkill without any result. The required files are available for download. I would think, that my code does the equivalent in this very case and it works just fine in under about a minute.

Pandas Version: 25.1.0
Pandas git version:  '171c71611886aab8549a8620c5b0071a129ad685'


#### Expected Output
No error and changed csv file


Discussion:
Can you post an example that we can copy/paste to replicate the behavior
I already posted the example in quotes. It is:

Import pandas

p_csv= pandas.read_csv(("/myDir/myFile.csv"), index_col=0)
pandas.get_dummies(p_csv, prefix="SEPARATOR_", columns="SEPARATOR")
p_csv.to_csv("/myDir/myNew.csv")
That code sample is not self-contained, so no one can copy / paste to replicate your issue. The below link might help:

http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports
alright.  here is a copy-pastable version with random data

###**CODE**
```
import pandas, numpy as np

columns = np.random.rand(145)
columns = columns.astype(str)
columns[110] = "SEPARATOR"
columns[0] = "INSTANCES"

separators = ["and", "clique", "gomory", "gomory1", "gomory2", "gomory3", "gomory4", "zerohalf"]
instances = ["021erkdjfiejrk", "5484ierdkj", "5487ehej"]

d = {}

for i in range(145):
    d[columns[i]]= np.random.rand(145000)

d["SEPARATOR"] = [np.random.choice(instances) for i in range(145000)]
d["INSTANCES"] = [np.random.choice(instances) for i in range(145000)]

p_csv= pd.DataFrame(d)
p_csv["SEPARATOR"]
print("getting dummies", p_csv.shape)
pd.get_dummies(p_csv, prefix="SEPARATOR_", columns="SEPARATOR")
print("success")
```
That is liable to produce a DataFrame with shape on the order of (145000, 145000), which I think is on the order of 80GB, which certainly won't fit on my laptop.  SIGKILL is likely coming from the OOM killer.
that is what I guessed what probably happened. But why would the api function do that when there are only 6 different values for separator?
(edited your comment to make it copy/paste more neatly, LMK if you object and I'll revert)
do you get the expected result if you pass `columns=["SEPARATOR"]` instead of `columns="SEPARATOR"`?
Yes! That worked, very fast too. Now that you mention it, I do see that the expected parameter is "list-like". Didn't expect that to be a problem, since I didn't get any error and also do get an error if I pass 'columns="fake_column"'
I think this should raise if `columns` is a string to avoid confusion like this. @TonyCongqianWang any interest in submitting a PR for that?
I will take a shot at this.

Resolving patch:
diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py
--- a/pandas/core/reshape/reshape.py
+++ b/pandas/core/reshape/reshape.py
@@ -863,6 +863,8 @@ def get_dummies(
         # determine columns being encoded
         if columns is None:
             data_to_encode = data.select_dtypes(include=dtypes_to_encode)
+        elif not is_list_like(columns):
+            raise TypeError("Input must be a list-like for parameter `columns`")
         else:
             data_to_encode = data[columns]
 


Test patch:
diff --git a/pandas/tests/reshape/test_reshape.py b/pandas/tests/reshape/test_reshape.py
--- a/pandas/tests/reshape/test_reshape.py
+++ b/pandas/tests/reshape/test_reshape.py
@@ -608,6 +608,23 @@ def test_get_dummies_all_sparse(self):
         )
         tm.assert_frame_equal(result, expected)
 
+    @pytest.mark.parametrize("values", ["baz"])
+    def test_get_dummies_with_string_values(self, values):
+        # issue #28383
+        df = pd.DataFrame(
+            {
+                "bar": [1, 2, 3, 4, 5, 6],
+                "foo": ["one", "one", "one", "two", "two", "two"],
+                "baz": ["A", "B", "C", "A", "B", "C"],
+                "zoo": ["x", "y", "z", "q", "w", "t"],
+            }
+        )
+
+        msg = "Input must be a list-like for parameter `columns`"
+
+        with pytest.raises(TypeError, match=msg):
+            pd.get_dummies(df, columns=values)
+
 
 class TestCategoricalReshape:
     def test_reshaping_multi_index_categorical(self):