From 79bfd6c7d4b38112349e049f517cd931d6edeb31 Mon Sep 17 00:00:00 2001 From: itholic Date: Wed, 10 Feb 2021 17:18:38 +0900 Subject: [PATCH 1/3] Fix bug on pow and rpow --- databricks/koalas/base.py | 14 ++++++++++++-- databricks/koalas/tests/test_series.py | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/databricks/koalas/base.py b/databricks/koalas/base.py index b8c8c5e21e..48ec1b3bdd 100644 --- a/databricks/koalas/base.py +++ b/databricks/koalas/base.py @@ -579,8 +579,18 @@ def rmod(left, right): return column_op(rmod)(self, other) - __pow__ = column_op(Column.__pow__) - __rpow__ = column_op(Column.__rpow__) + def __pow__(self, other) -> Union["Series", "Index"]: + def pow_func(left, right): + return F.when(F.lit(right is np.nan) & (left == 1), left).otherwise(pow(left, right)) + + return column_op(pow_func)(self, other) + + def __rpow__(self, other) -> Union["Series", "Index"]: + def rpow_func(left, right): + return F.when(left.isNull() & F.lit(right == 1), right).otherwise(pow(right, left)) + + return column_op(rpow_func)(self, other) + __abs__ = column_op(F.abs) # comparison operators diff --git a/databricks/koalas/tests/test_series.py b/databricks/koalas/tests/test_series.py index 8f18bf9e6d..90538779fd 100644 --- a/databricks/koalas/tests/test_series.py +++ b/databricks/koalas/tests/test_series.py @@ -2673,3 +2673,12 @@ def test_align(self): self.assert_eq(kdf_r, pdf_r) self.assertRaises(ValueError, lambda: kdf.a.align(kdf.b, axis=1)) + + def test_pow_and_rpow(self): + pser = pd.Series([1, 2, np.nan]) + kser = ks.from_pandas(pser) + + self.assert_eq(pser.pow(np.nan), kser.pow(np.nan)) + self.assert_eq(pser ** np.nan, kser ** np.nan) + self.assert_eq(pser.rpow(np.nan), kser.rpow(np.nan)) + self.assert_eq(1 ** pser, 1 ** kser) From 0ad24cf971ee85188782b6bdf81ff11023563e42 Mon Sep 17 00:00:00 2001 From: itholic Date: Thu, 11 Feb 2021 18:43:08 +0900 Subject: [PATCH 2/3] Addressed comments and added tests --- databricks/koalas/base.py | 4 ++-- .../koalas/tests/test_ops_on_diff_frames.py | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/databricks/koalas/base.py b/databricks/koalas/base.py index 48ec1b3bdd..eb0c431838 100644 --- a/databricks/koalas/base.py +++ b/databricks/koalas/base.py @@ -581,13 +581,13 @@ def rmod(left, right): def __pow__(self, other) -> Union["Series", "Index"]: def pow_func(left, right): - return F.when(F.lit(right is np.nan) & (left == 1), left).otherwise(pow(left, right)) + return F.when(left == 1, left).otherwise(Column.__pow__(left, right)) return column_op(pow_func)(self, other) def __rpow__(self, other) -> Union["Series", "Index"]: def rpow_func(left, right): - return F.when(left.isNull() & F.lit(right == 1), right).otherwise(pow(right, left)) + return F.when(F.lit(right == 1), right).otherwise(Column.__rpow__(left, right)) return column_op(rpow_func)(self, other) diff --git a/databricks/koalas/tests/test_ops_on_diff_frames.py b/databricks/koalas/tests/test_ops_on_diff_frames.py index c36d99147c..3bf023db3d 100644 --- a/databricks/koalas/tests/test_ops_on_diff_frames.py +++ b/databricks/koalas/tests/test_ops_on_diff_frames.py @@ -1518,6 +1518,16 @@ def test_align(self): self.assertRaises(ValueError, lambda: kdf1.align(kdf3, axis=None)) self.assertRaises(ValueError, lambda: kdf1.align(kdf3, axis=1)) + def test_pow_and_rpow(self): + pser = pd.Series([1, 2, np.nan]) + kser = ks.from_pandas(pser) + pser_other = pd.Series([np.nan, 2, 3]) + kser_other = ks.from_pandas(pser_other) + + self.assert_eq(pser.pow(pser_other), kser.pow(kser_other)) + self.assert_eq(pser ** pser_other, kser ** kser_other) + self.assert_eq(pser.rpow(pser_other), kser.rpow(kser_other)) + class OpsOnDiffFramesDisabledTest(ReusedSQLTestCase, SQLTestUtils): @classmethod @@ -1671,3 +1681,16 @@ def test_align(self): with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"): kdf1.align(kdf2, axis=0) + + def test_pow_and_rpow(self): + pser = pd.Series([1, 2, np.nan]) + kser = ks.from_pandas(pser) + pser_other = pd.Series([np.nan, 2, 3]) + kser_other = ks.from_pandas(pser_other) + + with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"): + kser.pow(kser_other) + with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"): + kser ** kser_other + with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"): + kser.rpow(kser_other) From 591246e0ed3b903afc2c19428ed67f3649a22bb2 Mon Sep 17 00:00:00 2001 From: itholic Date: Fri, 12 Feb 2021 20:57:04 +0900 Subject: [PATCH 3/3] Fix test --- databricks/koalas/tests/test_ops_on_diff_frames.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/databricks/koalas/tests/test_ops_on_diff_frames.py b/databricks/koalas/tests/test_ops_on_diff_frames.py index 3bf023db3d..36eacc3d5a 100644 --- a/databricks/koalas/tests/test_ops_on_diff_frames.py +++ b/databricks/koalas/tests/test_ops_on_diff_frames.py @@ -1524,9 +1524,9 @@ def test_pow_and_rpow(self): pser_other = pd.Series([np.nan, 2, 3]) kser_other = ks.from_pandas(pser_other) - self.assert_eq(pser.pow(pser_other), kser.pow(kser_other)) - self.assert_eq(pser ** pser_other, kser ** kser_other) - self.assert_eq(pser.rpow(pser_other), kser.rpow(kser_other)) + self.assert_eq(pser.pow(pser_other), kser.pow(kser_other).sort_index()) + self.assert_eq(pser ** pser_other, (kser ** kser_other).sort_index()) + self.assert_eq(pser.rpow(pser_other), kser.rpow(kser_other).sort_index()) class OpsOnDiffFramesDisabledTest(ReusedSQLTestCase, SQLTestUtils):