HyukjinKwon commented on a change in pull request #33882:
URL: https://github.com/apache/spark/pull/33882#discussion_r699817733



##########
File path: python/pyspark/pandas/namespace.py
##########
@@ -2814,9 +2824,18 @@ def to_numeric(arg):
     1.0
     """
     if isinstance(arg, Series):
-        return arg._with_new_scol(arg.spark.column.cast("float"))
+        if errors == "coerce":
+            return arg._with_new_scol(arg.spark.column.cast("int"))

Review comment:
       why should we change the type?

##########
File path: python/pyspark/pandas/namespace.py
##########
@@ -2814,9 +2824,18 @@ def to_numeric(arg):
     1.0
     """
     if isinstance(arg, Series):
-        return arg._with_new_scol(arg.spark.column.cast("float"))
+        if errors == "coerce":
+            return arg._with_new_scol(arg.spark.column.cast("int"))
+        elif errors == "ignore":
+            scol = arg.spark.column
+            casted_scol = scol.cast("int")
+            return arg._with_new_scol(F.when(casted_scol.isNull(), 
scol).otherwise(casted_scol))

Review comment:
       If `scol` is a string column, the output type can be a string column:
   
   ```python
   >>> from pyspark.sql import functions as F
   >>> scol = F.col("a")
   >>> casted_scol = scol.cast("int")
   >>> df = sql("SELECT 'a' as a")
   >>> df.select(F.when(casted_scol.isNull(), 
scol).otherwise(casted_scol)).printSchema()
   root
    |-- CASE WHEN (CAST(a AS INT) IS NULL) THEN a ELSE CAST(a AS INT) END: 
string (nullable = true)
   ```
   
   is this correct type?

##########
File path: python/pyspark/pandas/namespace.py
##########
@@ -2814,9 +2824,18 @@ def to_numeric(arg):
     1.0
     """
     if isinstance(arg, Series):
-        return arg._with_new_scol(arg.spark.column.cast("float"))
+        if errors == "coerce":
+            return arg._with_new_scol(arg.spark.column.cast("int"))
+        elif errors == "ignore":
+            scol = arg.spark.column
+            casted_scol = scol.cast("int")
+            return arg._with_new_scol(F.when(casted_scol.isNull(), 
scol).otherwise(casted_scol))
+        elif errors == "raise":
+            raise NotImplementedError("'raise' is not implemented yet, when 
the `arg` is Series.")

Review comment:
       Let's implement this case by using `assert_true` expression. e.g.) 
`assert_true(casted_col.isNotNull())`

##########
File path: python/pyspark/pandas/namespace.py
##########
@@ -2814,9 +2824,18 @@ def to_numeric(arg):
     1.0
     """
     if isinstance(arg, Series):
-        return arg._with_new_scol(arg.spark.column.cast("float"))
+        if errors == "coerce":
+            return arg._with_new_scol(arg.spark.column.cast("int"))
+        elif errors == "ignore":
+            scol = arg.spark.column
+            casted_scol = scol.cast("int")
+            return arg._with_new_scol(F.when(casted_scol.isNull(), 
scol).otherwise(casted_scol))

Review comment:
       when errors is `ignore`, it should return the input as is per the 
documentation. The problem is the type coercion will happen via Spark. For 
example, it throws an exception if you call `pd.to_numeric` with 
`datetime.datetime`s whereas pandas returns the input as is.

##########
File path: python/pyspark/pandas/namespace.py
##########
@@ -2814,9 +2824,18 @@ def to_numeric(arg):
     1.0
     """
     if isinstance(arg, Series):
-        return arg._with_new_scol(arg.spark.column.cast("float"))
+        if errors == "coerce":
+            return arg._with_new_scol(arg.spark.column.cast("int"))
+        elif errors == "ignore":
+            scol = arg.spark.column
+            casted_scol = scol.cast("int")
+            return arg._with_new_scol(F.when(casted_scol.isNull(), 
scol).otherwise(casted_scol))

Review comment:
       can you try this:
   
   ```python
   pd.to_numeric(pd.Series([datetime.datetime(1970, 1, 2)]), 
errors="ignore").to_list()
   ```




-- 
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]

Reply via email to