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 Series.first_valid_index #936

Merged
merged 6 commits into from
Oct 28, 2019
Merged
Changes from 1 commit
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
1 change: 0 additions & 1 deletion databricks/koalas/missing/series.py
Original file line number Diff line number Diff line change
@@ -69,7 +69,6 @@ class _MissingPandasLikeSeries(object):
ffill = unsupported_function('ffill')
filter = unsupported_function('filter')
first = unsupported_function('first')
first_valid_index = unsupported_function('first_valid_index')
get = unsupported_function('get')
infer_objects = unsupported_function('infer_objects')
interpolate = unsupported_function('interpolate')
59 changes: 59 additions & 0 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
@@ -3223,6 +3223,65 @@ def copy(self) -> 'Series':
"""
return _col(DataFrame(self._internal.copy()))

def first_valid_index(self):
"""
Retrieves the index of the first valid value.

Returns
-------
idx_first_valid : type of index

Examples
--------
>>> s = ks.Series([None, None, 3, 4, 5], index=[100, 200, 300, 400, 500])
>>> s
100 NaN
200 NaN
300 3.0
400 4.0
500 5.0
Name: 0, dtype: float64

>>> s.first_valid_index()
300

Support for MultiIndex

>>> midx = pd.MultiIndex([['lama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... [[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> s = ks.Series([None, None, None, None, 250, 1.5, 320, 1, 0.3], index=midx)
>>> s
lama speed NaN
weight NaN
length NaN
cow speed NaN
weight 250.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3
Name: 0, dtype: float64

>>> s.first_valid_index()
('cow', 'weight')
"""
sdf = self._internal.sdf
data_scol = self._internal.data_scols[0]

first_valid_value = sdf.select(
F.first(data_scol, True).alias(str(self.name))).first()[str(self.name)]
sdf = sdf.select(self._internal.index_scols) \
.where(data_scol == first_valid_value) \
.first()
first_valid_idx = tuple(sdf[idx_col] for idx_col in self._internal.index_columns)

if len(first_valid_idx) < 2:
first_valid_idx = first_valid_idx[0]

return first_valid_idx

# TODO: 'regex', 'method' parameter
def replace(self, to_replace=None, value=None, regex=False) -> 'Series':
"""
1 change: 1 addition & 0 deletions docs/source/reference/series.rst
Original file line number Diff line number Diff line change
@@ -184,6 +184,7 @@ Time series-related
:toctree: api/

Series.shift
Series.first_valid_index

Accessors
---------