Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Series.astype to work properly #1818

Merged
merged 3 commits into from
Oct 7, 2020
Merged
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
4 changes: 4 additions & 0 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
@@ -1027,6 +1027,10 @@ def astype(self, dtype) -> "Series":
scol = F.when(self.spark.column.isNull(), F.lit(False)).otherwise(
self.spark.column.cast(spark_type)
)
elif isinstance(spark_type, StringType):
scol = F.when(self.spark.column.isNull(), str(None)).otherwise(
self.spark.column.cast(spark_type)
)
else:
scol = self.spark.column.cast(spark_type)
return self._with_new_scol(scol)
3 changes: 3 additions & 0 deletions databricks/koalas/tests/test_series.py
Original file line number Diff line number Diff line change
@@ -1123,6 +1123,9 @@ def test_astype(self):
kser = ks.Series(pser)

self.assert_eq(kser.astype(bool), pser.astype(bool))
# TODO: restore after pandas 1.1.4 is released.
# self.assert_eq(kser.astype(str).tolist(), pser.astype(str).tolist())
self.assert_eq(kser.astype(str).tolist(), ["hi", "hi ", " ", " \t", "", "None"])
Comment on lines +1126 to +1128
Copy link
Contributor Author

@itholic itholic Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After pandas 1.1.4 is released and fixed related bug, I'll come back here to restore this.

Related issue: pandas-dev/pandas#36904 (comment)

self.assert_eq(kser.str.strip().astype(bool), pser.str.strip().astype(bool))

pser = pd.Series([True, False, None], name="x")