diff --git a/python/pyspark/pandas/data_type_ops/base.py b/python/pyspark/pandas/data_type_ops/base.py index b4a6b1abbcaf9..3cde6a6d62781 100644 --- a/python/pyspark/pandas/data_type_ops/base.py +++ b/python/pyspark/pandas/data_type_ops/base.py @@ -52,6 +52,7 @@ extension_object_dtypes_available, spark_type_to_pandas_dtype, ) +from pyspark.pandas.utils import is_ansi_mode_enabled if extension_dtypes_available: from pandas import Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype @@ -189,7 +190,10 @@ def _as_other_type(index_ops: IndexOpsLike, dtype: Dtype, spark_type: DataType) ) assert not need_pre_process, "Pre-processing is needed before the type casting." - scol = index_ops.spark.column.cast(spark_type) + if is_ansi_mode_enabled(index_ops._internal.spark_frame.sparkSession): + scol = index_ops.spark.column.try_cast(spark_type) + else: + scol = index_ops.spark.column.cast(spark_type) return index_ops._with_new_scol(scol, field=InternalField(dtype=dtype)) diff --git a/python/pyspark/pandas/tests/series/test_as_type.py b/python/pyspark/pandas/tests/series/test_as_type.py index dfd66ee05d382..91d6a7f3b3013 100644 --- a/python/pyspark/pandas/tests/series/test_as_type.py +++ b/python/pyspark/pandas/tests/series/test_as_type.py @@ -53,6 +53,7 @@ def test_astype(self): self.assert_eq(psser.astype(bool), pser.astype(bool)) self.assert_eq(psser.astype(str), pser.astype(str)) self.assert_eq(psser.str.strip().astype(bool), pser.str.strip().astype(bool)) + self.assert_eq(ps.Series(["abc"]).astype(int), pd.Series([float("nan")])) if extension_object_dtypes_available: from pandas import StringDtype