|
32 | 32 | from databricks.koalas.typedef import as_python_type |
33 | 33 | from pyspark import sql as spark |
34 | 34 | from pyspark.sql import functions as F, Column |
35 | | -from pyspark.sql.types import BooleanType, StructType, LongType, IntegerType |
| 35 | +from pyspark.sql.types import ( |
| 36 | + BooleanType, |
| 37 | + DoubleType, |
| 38 | + FloatType, |
| 39 | + StringType, |
| 40 | + StructType, |
| 41 | + LongType, |
| 42 | + IntegerType, |
| 43 | +) |
36 | 44 | from pyspark.sql.window import Window |
37 | 45 |
|
38 | 46 | from databricks import koalas as ks # For running doctests and reference resolution in PyCharm. |
@@ -918,7 +926,20 @@ def astype(self, dtype) -> "Series": |
918 | 926 | spark_type = as_spark_type(dtype) |
919 | 927 | if not spark_type: |
920 | 928 | raise ValueError("Type {} not understood".format(dtype)) |
921 | | - return self._with_new_scol(self._scol.cast(spark_type)) |
| 929 | + if isinstance(spark_type, BooleanType): |
| 930 | + if isinstance(self.spark_type, StringType): |
| 931 | + scol = F.when(self._scol.isNull(), F.lit(False)).otherwise(F.length(self._scol) > 0) |
| 932 | + elif isinstance(self.spark_type, (FloatType, DoubleType)): |
| 933 | + scol = F.when(self._scol.isNull() | F.isnan(self._scol), F.lit(True)).otherwise( |
| 934 | + self._scol.cast(spark_type) |
| 935 | + ) |
| 936 | + else: |
| 937 | + scol = F.when(self._scol.isNull(), F.lit(False)).otherwise( |
| 938 | + self._scol.cast(spark_type) |
| 939 | + ) |
| 940 | + else: |
| 941 | + scol = self._scol.cast(spark_type) |
| 942 | + return self._with_new_scol(scol) |
922 | 943 |
|
923 | 944 | def getField(self, name): |
924 | 945 | if not isinstance(self.spark_type, StructType): |
|
0 commit comments