Skip to content

Implement min, max, sum and mean of Rolling in Series and DataFrame #996

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

Merged
merged 7 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions databricks/koalas/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,9 @@ def median(self, accuracy=10000):
# This is expected to be small so it's fine to transpose.
return DataFrame(sdf)._to_internal_pandas().transpose().iloc[:, 0]

def rolling(self, *args, **kwargs):
return Rolling(self)
# TODO: 'center', 'win_type', 'on', 'axis' parameter should be implemented.
def rolling(self, window, min_periods=None):
return Rolling(self, window=window, min_periods=min_periods)

# TODO: 'center' and 'axis' parameter should be implemented.
# 'axis' implementation, refer https://github.com/databricks/koalas/pull/607
Expand Down
4 changes: 2 additions & 2 deletions databricks/koalas/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,8 @@ def nunique(self, dropna=True):
F.when(F.count(F.when(col.isNull(), 1).otherwise(None)) >= 1, 1).otherwise(0))
return self._reduce_for_stat_function(stat_function, only_numeric=False)

def rolling(self, *args, **kwargs):
return RollingGroupby(self)
def rolling(self, window, *args, **kwargs):
return RollingGroupby(self, window)

def expanding(self, *args, **kwargs):
return ExpandingGroupby(self)
Expand Down
8 changes: 0 additions & 8 deletions databricks/koalas/missing/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ class _MissingPandasLikeRolling(object):
count = unsupported_function_rolling("count")
cov = unsupported_function_rolling("cov")
kurt = unsupported_function_rolling("kurt")
max = unsupported_function_rolling("max")
mean = unsupported_function_rolling("mean")
median = unsupported_function_rolling("median")
min = unsupported_function_rolling("min")
quantile = unsupported_function_rolling("quantile")
skew = unsupported_function_rolling("skew")
std = unsupported_function_rolling("std")
sum = unsupported_function_rolling("sum")
validate = unsupported_function_rolling("validate")
var = unsupported_function_rolling("var")

Expand Down Expand Up @@ -120,14 +116,10 @@ class _MissingPandasLikeRollingGroupby(object):
count = unsupported_function_rolling("count")
cov = unsupported_function_rolling("cov")
kurt = unsupported_function_rolling("kurt")
max = unsupported_function_rolling("max")
mean = unsupported_function_rolling("mean")
median = unsupported_function_rolling("median")
min = unsupported_function_rolling("min")
quantile = unsupported_function_rolling("quantile")
skew = unsupported_function_rolling("skew")
std = unsupported_function_rolling("std")
sum = unsupported_function_rolling("sum")
validate = unsupported_function_rolling("validate")
var = unsupported_function_rolling("var")

Expand Down
55 changes: 55 additions & 0 deletions databricks/koalas/tests/test_rolling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import databricks.koalas as ks
from databricks.koalas.testing.utils import ReusedSQLTestCase, TestUtils
from databricks.koalas.window import Rolling


class RollingTests(ReusedSQLTestCase, TestUtils):

def test_rolling_error(self):
with self.assertRaisesRegex(ValueError, "window must be >= 0"):
ks.range(10).rolling(window=-1)
with self.assertRaisesRegex(ValueError, "min_periods must be >= 0"):
ks.range(10).rolling(window=1, min_periods=-1)

with self.assertRaisesRegex(
TypeError,
"kdf_or_kser must be a series or dataframe; however, got:.*int"):
Rolling(1, 2)

def _test_rolling_func(self, f):
kser = ks.Series([1, 2, 3])
pser = kser.to_pandas()
self.assert_eq(repr(getattr(kser.rolling(2), f)()), repr(getattr(pser.rolling(2), f)()))

kdf = ks.DataFrame({'a': [1, 2, 3, 2], 'b': [4.0, 2.0, 3.0, 1.0]})
pdf = kdf.to_pandas()
self.assert_eq(repr(getattr(kdf.rolling(2), f)()), repr(getattr(pdf.rolling(2), f)()))

def test_rolling_min(self):
self._test_rolling_func("min")

def test_rolling_max(self):
self._test_rolling_func("max")

def test_rolling_mean(self):
self._test_rolling_func("mean")

def test_rolling_sum(self):
self._test_rolling_func("sum")
Loading