Skip to content

Commit

Permalink
Add pow, rpow, mod, rmod, floordiv, rfloordiv in DataFrame and Series
Browse files Browse the repository at this point in the history
  • Loading branch information
HyukjinKwon committed Jun 26, 2019
1 parent a96c01f commit ea1fbe4
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 12 deletions.
9 changes: 9 additions & 0 deletions databricks/koalas/base.py
Expand Up @@ -161,6 +161,15 @@ def __radd__(self, other):
__rmul__ = _column_op(spark.Column.__rmul__)
__rdiv__ = _numpy_column_op(spark.Column.__rdiv__)
__rtruediv__ = _numpy_column_op(spark.Column.__rtruediv__)

def __floordiv__(self, other):
return self._with_new_scol(
F.floor(_numpy_column_op(spark.Column.__div__)(self, other)._scol))

def __rfloordiv__(self, other):
return self._with_new_scol(
F.floor(_numpy_column_op(spark.Column.__rdiv__)(self, other)._scol))

__rmod__ = _column_op(spark.Column.__rmod__)
__pow__ = _column_op(spark.Column.__pow__)
__rpow__ = _column_op(spark.Column.__rpow__)
Expand Down
90 changes: 90 additions & 0 deletions databricks/koalas/frame.py
Expand Up @@ -148,6 +148,24 @@
circle 0.0 360.0
triangle 3.0 180.0
rectangle 4.0 360.0
>>> df // 2
angles degrees
circle 0 180
triangle 1 90
rectangle 2 180
>>> df % 2
angles degrees
circle 0 0
triangle 1 0
rectangle 0 0
>>> df.pow(2)
angles degrees
circle 0.0 129600.0
triangle 9.0 32400.0
rectangle 16.0 129600.0
"""


Expand Down Expand Up @@ -351,6 +369,24 @@ def __sub__(self, other):
def __rsub__(self, other):
return self._map_series_op("rsub", other)

def __pow__(self, other):
return self._map_series_op("pow", other)

def __rpow__(self, other):
return self._map_series_op("rpow", other)

def __mod__(self, other):
return self._map_series_op("mod", other)

def __rmod__(self, other):
return self._map_series_op("rmod", other)

def __floordiv__(self, other):
return self._map_series_op("floordiv", other)

def __rfloordiv__(self, other):
return self._map_series_op("rfloordiv", other)

def add(self, other):
return self + other

Expand Down Expand Up @@ -447,6 +483,60 @@ def rsub(self, other):
equiv="other - dataframe",
reverse='sub')

def mod(self, other):
return self % other

mod.__doc__ = _flex_doc_FRAME.format(
desc='Modulo',
op_name='%',
equiv='dataframe % other',
reverse='rmod')

def rmod(self, other):
return other % self

rmod.__doc__ = _flex_doc_FRAME.format(
desc='Modulo',
op_name='%',
equiv='other % dataframe',
reverse='mod')

def pow(self, other):
return self ** other

pow.__doc__ = _flex_doc_FRAME.format(
desc='Exponential power of series',
op_name='**',
equiv='dataframe ** other',
reverse='rpow')

def rpow(self, other):
return other - self

rpow.__doc__ = _flex_doc_FRAME.format(
desc='Exponential power',
op_name='**',
equiv='other ** dataframe',
reverse='pow')

def floordiv(self, other):
return self // other

floordiv.__doc__ = _flex_doc_FRAME.format(
desc='Integer division',
op_name='//',
equiv='dataframe // other',
reverse='rfloordiv')

def rfloordiv(self, other):
return other - self

rfloordiv.__doc__ = _flex_doc_FRAME.format(
desc='Integer division',
op_name='//',
equiv='other // dataframe',
reverse='floordiv')

# Comparison Operators
def __eq__(self, other):
return self._map_series_op("eq", other)
Expand Down
6 changes: 0 additions & 6 deletions databricks/koalas/missing/frame.py
Expand Up @@ -76,7 +76,6 @@ class _MissingPandasLikeDataFrame(object):
filter = unsupported_function('filter')
first = unsupported_function('first')
first_valid_index = unsupported_function('first_valid_index')
floordiv = unsupported_function('floordiv')
get_values = unsupported_function('get_values')
hist = unsupported_function('hist')
idxmax = unsupported_function('idxmax')
Expand All @@ -96,13 +95,11 @@ class _MissingPandasLikeDataFrame(object):
mask = unsupported_function('mask')
median = unsupported_function('median')
memory_usage = unsupported_function('memory_usage')
mod = unsupported_function('mod')
mode = unsupported_function('mode')
pct_change = unsupported_function('pct_change')
pivot = unsupported_function('pivot')
pivot_table = unsupported_function('pivot_table')
pop = unsupported_function('pop')
pow = unsupported_function('pow')
prod = unsupported_function('prod')
product = unsupported_function('product')
quantile = unsupported_function('quantile')
Expand All @@ -116,11 +113,8 @@ class _MissingPandasLikeDataFrame(object):
reorder_levels = unsupported_function('reorder_levels')
replace = unsupported_function('replace')
resample = unsupported_function('resample')
rfloordiv = unsupported_function('rfloordiv')
rmod = unsupported_function('rmod')
rolling = unsupported_function('rolling')
round = unsupported_function('round')
rpow = unsupported_function('rpow')
select_dtypes = unsupported_function('select_dtypes')
sem = unsupported_function('sem')
set_axis = unsupported_function('set_axis')
Expand Down
6 changes: 0 additions & 6 deletions databricks/koalas/missing/series.py
Expand Up @@ -91,7 +91,6 @@ class _MissingPandasLikeSeries(object):
filter = unsupported_function('filter')
first = unsupported_function('first')
first_valid_index = unsupported_function('first_valid_index')
floordiv = unsupported_function('floordiv')
get = unsupported_function('get')
get_values = unsupported_function('get_values')
idxmax = unsupported_function('idxmax')
Expand All @@ -108,11 +107,9 @@ class _MissingPandasLikeSeries(object):
mask = unsupported_function('mask')
median = unsupported_function('median')
memory_usage = unsupported_function('memory_usage')
mod = unsupported_function('mod')
mode = unsupported_function('mode')
pct_change = unsupported_function('pct_change')
pop = unsupported_function('pop')
pow = unsupported_function('pow')
prod = unsupported_function('prod')
product = unsupported_function('product')
ptp = unsupported_function('ptp')
Expand All @@ -128,11 +125,8 @@ class _MissingPandasLikeSeries(object):
repeat = unsupported_function('repeat')
replace = unsupported_function('replace')
resample = unsupported_function('resample')
rfloordiv = unsupported_function('rfloordiv')
rmod = unsupported_function('rmod')
rolling = unsupported_function('rolling')
round = unsupported_function('round')
rpow = unsupported_function('rpow')
searchsorted = unsupported_function('searchsorted')
sem = unsupported_function('sem')
set_axis = unsupported_function('set_axis')
Expand Down
123 changes: 123 additions & 0 deletions databricks/koalas/series.py
Expand Up @@ -152,6 +152,69 @@
Name: a, dtype: float64
"""

_pow_example_SERIES = """
Examples
--------
>>> df = ks.DataFrame({'a': [2, 2, 4, np.nan],
... 'b': [2, np.nan, 2, np.nan]},
... index=['a', 'b', 'c', 'd'], columns=['a', 'b'])
>>> df
a b
a 2.0 2.0
b 2.0 NaN
c 4.0 2.0
d NaN NaN
>>> df.a.pow(df.b)
a 4.0
b NaN
c 16.0
d NaN
Name: a, dtype: float64
"""

_mod_example_SERIES = """
Examples
--------
>>> df = ks.DataFrame({'a': [2, 2, 4, np.nan],
... 'b': [2, np.nan, 2, np.nan]},
... index=['a', 'b', 'c', 'd'], columns=['a', 'b'])
>>> df
a b
a 2.0 2.0
b 2.0 NaN
c 4.0 2.0
d NaN NaN
>>> df.a.mod(df.b)
a 0.0
b NaN
c 0.0
d NaN
Name: a, dtype: float64
"""

_floordiv_example_SERIES = """
Examples
--------
>>> df = ks.DataFrame({'a': [2, 2, 4, np.nan],
... 'b': [2, np.nan, 2, np.nan]},
... index=['a', 'b', 'c', 'd'], columns=['a', 'b'])
>>> df
a b
a 2.0 2.0
b 2.0 NaN
c 4.0 2.0
d NaN NaN
>>> df.a.floordiv(df.b)
a 1.0
b NaN
c 2.0
d NaN
Name: a, dtype: float64
"""


# Needed to disambiguate Series.str and str type
str_type = str
Expand Down Expand Up @@ -347,6 +410,66 @@ def rsub(self, other):
reverse='sub',
series_examples=_sub_example_SERIES)

def mod(self, other):
return (self % other).rename(self.name)

mod.__doc__ = _flex_doc_SERIES.format(
desc='Modulo',
op_name='%',
equiv='series % other',
reverse='rmod',
series_examples=_mod_example_SERIES)

def rmod(self, other):
return (other % self).rename(self.name)

rmod.__doc__ = _flex_doc_SERIES.format(
desc='Modulo',
op_name='%',
equiv='other % series',
reverse='mod',
series_examples=_mod_example_SERIES)

def pow(self, other):
return (self ** other).rename(self.name)

pow.__doc__ = _flex_doc_SERIES.format(
desc='Exponential power of series',
op_name='**',
equiv='series ** other',
reverse='rpow',
series_examples=_pow_example_SERIES)

def rpow(self, other):
return (other - self).rename(self.name)

rpow.__doc__ = _flex_doc_SERIES.format(
desc='Exponential power',
op_name='**',
equiv='other ** series',
reverse='pow',
series_examples=_pow_example_SERIES)

def floordiv(self, other):
return (self // other).rename(self.name)

floordiv.__doc__ = _flex_doc_SERIES.format(
desc='Integer division',
op_name='//',
equiv='series // other',
reverse='rfloordiv',
series_examples=_floordiv_example_SERIES)

def rfloordiv(self, other):
return (other - self).rename(self.name)

rfloordiv.__doc__ = _flex_doc_SERIES.format(
desc='Integer division',
op_name='//',
equiv='other // series',
reverse='floordiv',
series_examples=_floordiv_example_SERIES)

# Comparison Operators
def eq(self, other):
"""
Expand Down
6 changes: 6 additions & 0 deletions docs/source/reference/frame.rst
Expand Up @@ -68,6 +68,12 @@ Binary operator functions
DataFrame.rmul
DataFrame.sub
DataFrame.rsub
DataFrame.pow
DataFrame.rpow
DataFrame.mod
DataFrame.rmod
DataFrame.floordiv
DataFrame.rfloordiv
DataFrame.lt
DataFrame.gt
DataFrame.le
Expand Down
6 changes: 6 additions & 0 deletions docs/source/reference/series.rst
Expand Up @@ -63,6 +63,12 @@ Binary operator functions
Series.rtruediv
Series.sub
Series.truediv
Series.pow
Series.rpow
Series.mod
Series.rmod
Series.floordiv
Series.rfloordiv
Series.lt
Series.gt
Series.le
Expand Down

0 comments on commit ea1fbe4

Please sign in to comment.