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

Implement Index.fillna #1102

Merged
merged 2 commits into from
Dec 5, 2019
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
30 changes: 30 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,36 @@ def rename(self, name: Union[str, Tuple[str, ...]], inplace: bool = False):
else:
return Index(DataFrame(internal), scol=self._scol)

# TODO: add downcast parameter for fillna function
def fillna(self, value):
"""
Fill NA/NaN values with the specified value.

Parameters
----------
value : scalar
Scalar value to use to fill holes (e.g. 0). This value cannot be a list-likes.

Returns
-------
Index :
filled with value

Examples
--------
>>> ki = ks.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 2, None]).index
>>> ki
Float64Index([1.0, 2.0, nan], dtype='float64')

>>> ki.fillna(0)
Float64Index([1.0, 2.0, 0.0], dtype='float64')
"""
if not isinstance(value, (float, int, str, bool)):
raise TypeError("Unsupported type %s" % type(value))
sdf = self._internal.sdf.fillna(value)
result = DataFrame(self._kdf._internal.copy(sdf=sdf)).index
return result

def to_series(self, name: Union[str, Tuple[str, ...]] = None) -> Series:
"""
Create a Series with both index and values equal to the index keys
Expand Down
2 changes: 0 additions & 2 deletions databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _MissingPandasLikeIndex(object):
duplicated = unsupported_function('duplicated')
equals = unsupported_function('equals')
factorize = unsupported_function('factorize')
fillna = unsupported_function('fillna')
format = unsupported_function('format')
get_indexer = unsupported_function('get_indexer')
get_indexer_for = unsupported_function('get_indexer_for')
Expand Down Expand Up @@ -145,7 +144,6 @@ class _MissingPandasLikeMultiIndex(object):
equal_levels = unsupported_function('equal_levels')
equals = unsupported_function('equals')
factorize = unsupported_function('factorize')
fillna = unsupported_function('fillna')
format = unsupported_function('format')
get_indexer = unsupported_function('get_indexer')
get_indexer_for = unsupported_function('get_indexer_for')
Expand Down
10 changes: 10 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,13 @@ def test_multiindex_from_arrays(self):
kidx = ks.MultiIndex.from_arrays(arrays)

self.assert_eq(pidx, kidx)

def test_index_fillna(self):
pidx = pd.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 2, None]).index
kidx = ks.DataFrame({'a': ['a', 'b', 'c']}, index=[1, 2, None]).index

self.assert_eq(pidx.fillna(0), kidx.fillna(0))
self.assert_eq(pidx.rename('name').fillna(0), kidx.rename('name').fillna(0))

RainFung marked this conversation as resolved.
Show resolved Hide resolved
with self.assertRaisesRegex(TypeError, "Unsupported type <class 'list'>"):
kidx.fillna([1, 2])
1 change: 1 addition & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Missing Values
.. autosummary::
:toctree: api/

Index.fillna
Index.dropna
Index.isna
Index.notna
Expand Down