Repository: databricks/koalas

Issue:
Series.isin() throws exception when fed numpy array
Running Spark 3.1.1 with Pyspark 3.1.1. and Koalas 1.7.0
```
df = pd.DataFrame({'a': [1, 2, 3], 'b': [2, 3, 4]})
numpy_arr = np.array([3, 4, 5])
df['a'].isin(numpy_arr)
```
works

```
kdf = ks.from_pandas(df)
kdf[kdf['a'].isin(numpy_arr)]
```

throws `AttributeError: 'numpy.int64' object has no attribute '_get_object_id'`

using `numpy_arr.tolist()` fixes it. Is this deviation from the Pandas API intended?


Discussion:

Resolving patch:
diff --git a/databricks/koalas/base.py b/databricks/koalas/base.py
--- a/databricks/koalas/base.py
+++ b/databricks/koalas/base.py
@@ -1161,7 +1161,7 @@ def isin(self, values) -> Union["Series", "Index"]:
 
         Parameters
         ----------
-        values : list or set
+        values : set or list-like
             The sequence of values to test.
 
         Returns
@@ -1202,7 +1202,8 @@ def isin(self, values) -> Union["Series", "Index"]:
                 " to isin(), you passed a [{values_type}]".format(values_type=type(values).__name__)
             )
 
-        return self._with_new_scol(self.spark.column.isin(list(values)))
+        values = values.tolist() if isinstance(values, np.ndarray) else list(values)
+        return self._with_new_scol(self.spark.column.isin(values))
 
     def isnull(self) -> Union["Series", "Index"]:
         """
diff --git a/databricks/koalas/frame.py b/databricks/koalas/frame.py
--- a/databricks/koalas/frame.py
+++ b/databricks/koalas/frame.py
@@ -7090,9 +7090,11 @@ def isin(self, values) -> "DataFrame":
         if isinstance(values, dict):
             for i, col in enumerate(self.columns):
                 if col in values:
+                    item = values[col]
+                    item = item.tolist() if isinstance(item, np.ndarray) else list(item)
                     data_spark_columns.append(
                         self._internal.spark_column_for(self._internal.column_labels[i])
-                        .isin(values[col])
+                        .isin(item)
                         .alias(self._internal.data_spark_column_names[i])
                     )
                 else:
@@ -7100,9 +7102,10 @@ def isin(self, values) -> "DataFrame":
                         F.lit(False).alias(self._internal.data_spark_column_names[i])
                     )
         elif is_list_like(values):
+            values = values.tolist() if isinstance(values, np.ndarray) else list(values)
             data_spark_columns += [
                 self._internal.spark_column_for(label)
-                .isin(list(values))
+                .isin(values)
                 .alias(self._internal.spark_column_name_for(label))
                 for label in self._internal.column_labels
             ]


Test patch:
diff --git a/databricks/koalas/tests/test_dataframe.py b/databricks/koalas/tests/test_dataframe.py
--- a/databricks/koalas/tests/test_dataframe.py
+++ b/databricks/koalas/tests/test_dataframe.py
@@ -1848,10 +1848,16 @@ def test_isin(self):
         kdf = ks.from_pandas(pdf)
 
         self.assert_eq(kdf.isin([4, "six"]), pdf.isin([4, "six"]))
+        # Seems like pandas has a bug when passing `np.array` as parameter
+        self.assert_eq(kdf.isin(np.array([4, "six"])), pdf.isin([4, "six"]))
         self.assert_eq(
             kdf.isin({"a": [2, 8], "c": ["three", "one"]}),
             pdf.isin({"a": [2, 8], "c": ["three", "one"]}),
         )
+        self.assert_eq(
+            kdf.isin({"a": np.array([2, 8]), "c": ["three", "one"]}),
+            pdf.isin({"a": np.array([2, 8]), "c": ["three", "one"]}),
+        )
 
         msg = "'DataFrame' object has no attribute {'e'}"
         with self.assertRaisesRegex(AttributeError, msg):
diff --git a/databricks/koalas/tests/test_series.py b/databricks/koalas/tests/test_series.py
--- a/databricks/koalas/tests/test_series.py
+++ b/databricks/koalas/tests/test_series.py
@@ -419,6 +419,7 @@ def test_isin(self):
         kser = ks.from_pandas(pser)
 
         self.assert_eq(kser.isin(["cow", "lama"]), pser.isin(["cow", "lama"]))
+        self.assert_eq(kser.isin(np.array(["cow", "lama"])), pser.isin(np.array(["cow", "lama"])))
         self.assert_eq(kser.isin({"cow"}), pser.isin({"cow"}))
 
         msg = "only list-like objects are allowed to be passed to isin()"