zhengruifeng commented on code in PR #37635:
URL: https://github.com/apache/spark/pull/37635#discussion_r955553096
##########
python/pyspark/sql/types.py:
##########
@@ -2268,12 +2268,40 @@ def convert(self, obj: "np.generic", gateway_client:
GatewayClient) -> Any:
return obj.item()
+class NumpyArrayConverter:
+ def can_convert(self, obj: Any) -> bool:
+ return has_numpy and isinstance(obj, np.ndarray)
Review Comment:
do we need to check the shape here?
the `obj` may be a tensor like :
```
In [11]: obj = np.zeros([2,3,4,5])
In [12]: obj.shape
Out[12]: (2, 3, 4, 5)
```
##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1003,6 +1003,30 @@ def test_np_scalar_input(self):
res = df.select(array_position(df.data,
dtype(1)).alias("c")).collect()
self.assertEqual([Row(c=1), Row(c=0)], res)
+ @unittest.skipIf(not have_numpy, "NumPy not installed")
+ def test_ndarray_input(self):
+ import numpy as np
+
+ int_arrs = [np.array([1, 2]).astype(t) for t in ["int8", "int16",
"int32", "int64"]]
+ for arr in int_arrs:
+ self.assertEqual(
+ [Row(b=[1, 2])],
self.spark.range(1).select(lit(arr).alias("b")).collect()
+ )
+
+ float_arrs = [np.array([0.1, 0.2]).astype(t) for t in ["float32",
"float64"]]
+
+ self.assertEqual(
+ [("b", "array<double>")],
+ self.spark.range(1).select(lit(float_arrs[0]).alias("b")).dtypes,
+ )
+ self.assertEqual(
Review Comment:
What about adding a few tests which use array function?
for example: `concat` / `array_intersect`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]