diff --git a/databricks/koalas/indexes.py b/databricks/koalas/indexes.py index 386a5e3047..16a823c7a1 100644 --- a/databricks/koalas/indexes.py +++ b/databricks/koalas/indexes.py @@ -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 diff --git a/databricks/koalas/missing/indexes.py b/databricks/koalas/missing/indexes.py index 9ef57c280f..b9b98de975 100644 --- a/databricks/koalas/missing/indexes.py +++ b/databricks/koalas/missing/indexes.py @@ -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') @@ -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') diff --git a/databricks/koalas/tests/test_indexes.py b/databricks/koalas/tests/test_indexes.py index 59a438696e..df518e37c3 100644 --- a/databricks/koalas/tests/test_indexes.py +++ b/databricks/koalas/tests/test_indexes.py @@ -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)) + + with self.assertRaisesRegex(TypeError, "Unsupported type "): + kidx.fillna([1, 2]) diff --git a/docs/source/reference/indexing.rst b/docs/source/reference/indexing.rst index b1f7f237e8..0da4c41b7f 100644 --- a/docs/source/reference/indexing.rst +++ b/docs/source/reference/indexing.rst @@ -47,6 +47,7 @@ Missing Values .. autosummary:: :toctree: api/ + Index.fillna Index.dropna Index.isna Index.notna