Repository: databricks/koalas

Issue:
Top-level Pandas conversion function - to_numeric
Implement `pandas.to_numeric`

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_numeric.html


Discussion:
Basically, i think koalas doesn't support multi-type for `Series` or `DataFrame` since it works internally with spark dataframe that doesn't support multi-type for column.
cc. @HyukjinKwon @ueshin if it's right, maybe we explicitly wouldn't support this function?
That's correct that we can't support the examples provided. However, there's valid cases, for instance, convert a string column to a numeric column.
@HyukjinKwon ah, that makes sense.
okay then i'm working on this from now on.
Thanks for the comment!

Resolving patch:
diff --git a/databricks/koalas/namespace.py b/databricks/koalas/namespace.py
--- a/databricks/koalas/namespace.py
+++ b/databricks/koalas/namespace.py
@@ -44,7 +44,7 @@
 __all__ = ["from_pandas", "range", "read_csv", "read_delta", "read_table", "read_spark_io",
            "read_parquet", "read_clipboard", "read_excel", "read_html", "to_datetime",
            "get_dummies", "concat", "melt", "isna", "isnull", "notna", "notnull",
-           "read_sql_table", "read_sql_query", "read_sql", "read_json", "merge"]
+           "read_sql_table", "read_sql_query", "read_sql", "read_json", "merge", "to_numeric"]
 
 
 def from_pandas(pobj: Union['pd.DataFrame', 'pd.Series']) -> Union['Series', 'DataFrame']:
@@ -1863,6 +1863,78 @@ def merge(obj, right: 'DataFrame', how: str = 'inner',
         left_index=left_index, right_index=right_index, suffixes=suffixes)
 
 
+def to_numeric(arg):
+    """
+    Convert argument to a numeric type.
+
+    Parameters
+    ----------
+    arg : scalar, list, tuple, 1-d array, or Series
+
+    Returns
+    -------
+    ret : numeric if parsing succeeded.
+
+    See Also
+    --------
+    DataFrame.astype : Cast argument to a specified dtype.
+    to_datetime : Convert argument to datetime.
+    to_timedelta : Convert argument to timedelta.
+    numpy.ndarray.astype : Cast a numpy array to a specified type.
+
+    Examples
+    --------
+
+    >>> kser = ks.Series(['1.0', '2', '-3'])
+    >>> kser
+    0    1.0
+    1      2
+    2     -3
+    Name: 0, dtype: object
+
+    >>> ks.to_numeric(kser)
+    0    1.0
+    1    2.0
+    2   -3.0
+    Name: 0, dtype: float32
+
+    If given Series contains invalid value to cast float, just cast it to `np.nan`
+
+    >>> kser = ks.Series(['apple', '1.0', '2', '-3'])
+    >>> kser
+    0    apple
+    1      1.0
+    2        2
+    3       -3
+    Name: 0, dtype: object
+
+    >>> ks.to_numeric(kser)
+    0    NaN
+    1    1.0
+    2    2.0
+    3   -3.0
+    Name: 0, dtype: float32
+
+    Also support for list, tuple, np.array, or a scalar
+
+    >>> ks.to_numeric(['1.0', '2', '-3'])
+    array([ 1.,  2., -3.])
+
+    >>> ks.to_numeric(('1.0', '2', '-3'))
+    array([ 1.,  2., -3.])
+
+    >>> ks.to_numeric(np.array(['1.0', '2', '-3']))
+    array([ 1.,  2., -3.])
+
+    >>> ks.to_numeric('1.0')
+    1.0
+    """
+    if isinstance(arg, Series):
+        return arg._with_new_scol(arg._internal.scol.cast('float'))
+    else:
+        return pd.to_numeric(arg)
+
+
 # @pandas_wraps(return_col=np.datetime64)
 @pandas_wraps
 def _to_datetime1(arg, errors, format, unit, infer_datetime_format,


Test patch: