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
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
1 change: 0 additions & 1 deletion databricks/koalas/missing/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
56 changes: 56 additions & 0 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,62 @@ def mode(self, dropna=True) -> 'Series':

return result

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.scol

first_valid_row = sdf.where(data_scol.isNotNull()).first()
first_valid_idx = tuple(first_valid_row[idx_col]
for idx_col in self._internal.index_columns)

if len(first_valid_idx) == 1:
first_valid_idx = first_valid_idx[0]

return first_valid_idx

def keys(self):
"""
Return alias for index.
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 @@ -188,6 +188,7 @@ Time series-related
:toctree: api/

Series.shift
Series.first_valid_index

Accessors
---------
Expand Down