Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion python/pyspark/pandas/data_type_ops/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))


Expand Down
1 change: 1 addition & 0 deletions python/pyspark/pandas/tests/series/test_as_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down