Skip to content
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

Added backfill for Series & DataFrame #1798

Merged
merged 4 commits into from
Sep 26, 2020
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
2 changes: 2 additions & 0 deletions databricks/koalas/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,8 @@ def bfill(self, axis=None, inplace=False, limit=None):
"""
return self.fillna(method="bfill", axis=axis, inplace=inplace, limit=limit)

backfill = bfill

# TODO: add 'downcast' when value parameter exists
def ffill(self, axis=None, inplace=False, limit=None):
"""
Expand Down
1 change: 0 additions & 1 deletion databricks/koalas/missing/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class _MissingPandasLikeDataFrame(object):
asfreq = _unsupported_function("asfreq")
asof = _unsupported_function("asof")
at_time = _unsupported_function("at_time")
backfill = _unsupported_function("_backfill")
between_time = _unsupported_function("between_time")
boxplot = _unsupported_function("boxplot")
combine = _unsupported_function("combine")
Expand Down
1 change: 0 additions & 1 deletion databricks/koalas/missing/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class MissingPandasLikeSeries(object):
asfreq = _unsupported_function("asfreq")
at_time = _unsupported_function("at_time")
autocorr = _unsupported_function("autocorr")
backfill = _unsupported_function("backfill")
between_time = _unsupported_function("between_time")
combine = _unsupported_function("combine")
compare = _unsupported_function("compare")
Expand Down
35 changes: 35 additions & 0 deletions databricks/koalas/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4132,3 +4132,38 @@ def test_pad(self):
# Test `inplace=True`
kdf.pad(inplace=True)
self.assert_eq(expected, kdf)

def test_backfill(self):
pdf = pd.DataFrame(
{
"A": [None, 3, None, None],
"B": [2, 4, None, 3],
"C": [None, None, None, 1],
"D": [0, 1, 5, 4],
},
columns=["A", "B", "C", "D"],
)
kdf = ks.from_pandas(pdf)

if LooseVersion(pd.__version__) >= LooseVersion("1.1"):
self.assert_eq(pdf.backfill(), kdf.backfill())

# Test `inplace=True`
pdf.backfill(inplace=True)
kdf.backfill(inplace=True)
self.assert_eq(pdf, kdf)
else:
expected = ks.DataFrame(
{
"A": [3.0, 3.0, None, None],
"B": [2.0, 4.0, 3.0, 3.0],
"C": [1.0, 1.0, 1.0, 1.0],
"D": [0, 1, 5, 4],
},
columns=["A", "B", "C", "D"],
)
self.assert_eq(expected, kdf.backfill())

# Test `inplace=True`
kdf.backfill(inplace=True)
self.assert_eq(expected, kdf)
19 changes: 19 additions & 0 deletions databricks/koalas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,3 +2207,22 @@ def test_argmin_argmax(self):
ks.Series([]).argmin()
with self.assertRaisesRegex(ValueError, "attempt to get argmax of an empty sequence"):
ks.Series([]).argmax()

def test_backfill(self):
pser = pd.Series([np.nan, 2, 3, 4, np.nan, 6], name="x")
kser = ks.from_pandas(pser)

if LooseVersion(pd.__version__) >= LooseVersion("1.1"):
self.assert_eq(pser.backfill(), kser.backfill())

# Test `inplace=True`
pser.backfill(inplace=True)
kser.backfill(inplace=True)
self.assert_eq(pser, kser)
else:
expected = ks.Series([2.0, 2.0, 3.0, 4.0, 6.0, 6.0], name="x")
self.assert_eq(expected, kser.backfill())

# Test `inplace=True`
kser.backfill(inplace=True)
self.assert_eq(expected, kser)
1 change: 1 addition & 0 deletions docs/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Missing data handling
.. autosummary::
:toctree: api/

DataFrame.backfill
DataFrame.dropna
DataFrame.fillna
DataFrame.replace
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Missing data handling
.. autosummary::
:toctree: api/

Series.backfill
Series.isna
Series.isnull
Series.notna
Expand Down