Skip to content
Merged
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
28 changes: 1 addition & 27 deletions databricks/koalas/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@
from pyspark.sql.readwriter import OptionUtils
from pyspark.sql.types import (
BooleanType,
ByteType,
DecimalType,
DoubleType,
FloatType,
IntegerType,
LongType,
NumericType,
ShortType,
StructType,
StructField,
)
Expand Down Expand Up @@ -5708,28 +5703,7 @@ def clip(self, lower: Union[float, int] = None, upper: Union[float, int] = None)
if lower is None and upper is None:
return self

numeric_types = (
DecimalType,
DoubleType,
FloatType,
ByteType,
IntegerType,
LongType,
ShortType,
)

def op(kser):
if isinstance(kser.spark_type, numeric_types):
scol = kser.spark_column
if lower is not None:
scol = F.when(scol < lower, lower).otherwise(scol)
if upper is not None:
scol = F.when(scol > upper, upper).otherwise(scol)
return scol.alias(kser._internal.data_spark_column_names[0])
else:
return kser

return self._apply_series_op(op)
return self._apply_series_op(lambda kser: kser.clip(lower=lower, upper=upper))

def head(self, n: int = 5) -> "DataFrame":
"""
Expand Down
23 changes: 20 additions & 3 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
BooleanType,
DoubleType,
FloatType,
IntegerType,
LongType,
NumericType,
StringType,
StructType,
LongType,
IntegerType,
)
from pyspark.sql.window import Window

Expand Down Expand Up @@ -1726,7 +1727,23 @@ def clip(self, lower: Union[float, int] = None, upper: Union[float, int] = None)
instances of 'str' and 'int'" while `ks.Series(['a', 'b']).clip(0, 1)` will output the
original Series, simply ignoring the incompatible types.
"""
return first_series(self.to_dataframe().clip(lower, upper))
if is_list_like(lower) or is_list_like(upper):
raise ValueError(
"List-like value are not supported for 'lower' and 'upper' at the " + "moment"
)

if lower is None and upper is None:
return self

if isinstance(self.spark_type, NumericType):
scol = self.spark_column
if lower is not None:
scol = F.when(scol < lower, lower).otherwise(scol)
if upper is not None:
scol = F.when(scol > upper, upper).otherwise(scol)
return self._with_new_scol(scol.alias(self._internal.data_spark_column_names[0]))
else:
return self

def drop(
self,
Expand Down
4 changes: 4 additions & 0 deletions databricks/koalas/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,10 @@ def test_clip(self):
# Assert lower and upper
self.assert_eq(kdf.clip(1, 3), pdf.clip(1, 3))

pdf["clip"] = pdf.A.clip(lower=1, upper=3)
kdf["clip"] = kdf.A.clip(lower=1, upper=3)
self.assert_eq(kdf, pdf)

# Assert behavior on string values
str_kdf = ks.DataFrame({"A": ["a", "b", "c"]}, index=np.random.rand(3))
self.assert_eq(str_kdf.clip(1, 3), str_kdf)
Expand Down