Skip to content

Commit 4c86f3c

Browse files
authored
Fix Frame.abs to support bool type and disallow non-numeric types. (#1980)
Fixes `Frame.abs` to support bool type and disallow non-numeric types.
1 parent f9465aa commit 4c86f3c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

databricks/koalas/generic.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,18 @@ def abs(self) -> Union["DataFrame", "Series"]:
17481748
2 6 30 30
17491749
3 7 40 50
17501750
"""
1751-
return self._apply_series_op(lambda kser: kser._with_new_scol(F.abs(kser.spark.column)))
1751+
1752+
def abs(kser):
1753+
if isinstance(kser.spark.data_type, BooleanType):
1754+
return kser
1755+
elif isinstance(kser.spark.data_type, NumericType):
1756+
return kser.spark.transform(F.abs)
1757+
else:
1758+
raise TypeError(
1759+
"bad operand type for abs(): {}".format(kser.spark.data_type.simpleString())
1760+
)
1761+
1762+
return self._apply_series_op(abs)
17521763

17531764
# TODO: by argument only support the grouping name and as_index only for now. Documentation
17541765
# should be updated when it's supported.

databricks/koalas/tests/test_stats.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,26 @@ def test_sum(self):
9494
def test_abs(self):
9595
pdf = pd.DataFrame(
9696
{
97-
"A": [1, -2, 3, -4, 5],
98-
"B": [1.0, -2, 3, -4, 5],
99-
"C": [-6.0, -7, -8, -9, 10],
100-
"D": ["a", "b", "c", "d", "e"],
97+
"A": [1, -2, np.nan, -4, 5],
98+
"B": [1.0, -2, np.nan, -4, 5],
99+
"C": [-6.0, -7, -8, np.nan, 10],
100+
"D": ["a", "b", "c", "d", np.nan],
101+
"E": [True, np.nan, False, True, True],
101102
}
102103
)
103104
kdf = ks.from_pandas(pdf)
104105
self.assert_eq(kdf.A.abs(), pdf.A.abs())
105106
self.assert_eq(kdf.B.abs(), pdf.B.abs())
107+
self.assert_eq(kdf.E.abs(), pdf.E.abs())
108+
# pandas' bug?
109+
# self.assert_eq(kdf[["B", "C", "E"]].abs(), pdf[["B", "C", "E"]].abs())
106110
self.assert_eq(kdf[["B", "C"]].abs(), pdf[["B", "C"]].abs())
111+
self.assert_eq(kdf[["E"]].abs(), pdf[["E"]].abs())
112+
113+
with self.assertRaisesRegex(TypeError, "bad operand type for abs\\(\\): string"):
114+
kdf.abs()
115+
with self.assertRaisesRegex(TypeError, "bad operand type for abs\\(\\): string"):
116+
kdf.D.abs()
107117

108118
def test_axis_on_dataframe(self):
109119
# The number of each count is intentionally big

0 commit comments

Comments
 (0)