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

Fix first/last_valid_index to support empty column DataFrame. #1923

Merged
merged 4 commits into from
Nov 23, 2020

Conversation

ueshin
Copy link
Collaborator

@ueshin ueshin commented Nov 21, 2020

Fixes first/last_valid_index to support empty column DataFrame.

E.g.,:

>>> kdf = ks.DataFrame({"a": [None, 2, 3, 2], "b": [None, 2.0, 3.0, 1.0], "c": [None, 200, 400, 200]}, index=["Q", "W", "E", "R"])
>>> kdf
     a    b      c
Q  NaN  NaN    NaN
W  2.0  2.0  200.0
E  3.0  3.0  400.0
R  2.0  1.0  200.0
>>> kdf.first_valid_index()
'W'
>>> kdf[[]].first_valid_index()
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value

, which should return None:

>>> kdf[[]].to_pandas().first_valid_index()

This PR also includes:

  • small optimizations
  • explicitly raise an error in last_valid_index for PySpark<3.0
  • explicitly make tests skip for PySpark<3.0

@ueshin ueshin requested a review from xinrong-meng November 21, 2020 02:01
@codecov-io
Copy link

codecov-io commented Nov 21, 2020

Codecov Report

Merging #1923 (5af71a4) into master (c6093b9) will decrease coverage by 0.00%.
The diff coverage is 95.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1923      +/-   ##
==========================================
- Coverage   94.15%   94.14%   -0.01%     
==========================================
  Files          41       41              
  Lines       10007    10011       +4     
==========================================
+ Hits         9422     9425       +3     
- Misses        585      586       +1     
Impacted Files Coverage Δ
databricks/koalas/generic.py 92.91% <95.65%> (-0.23%) ⬇️
databricks/koalas/namespace.py 84.23% <0.00%> (ø)
databricks/koalas/groupby.py 91.42% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c6093b9...5af71a4. Read the comment docs.

@@ -1687,7 +1688,7 @@ def bool(self) -> bool:
raise TypeError("bool() expects DataFrame or Series; however, " "got [%s]" % (self,))
return df.head(2)._to_internal_pandas().bool()

def first_valid_index(self) -> Union[Any, Tuple[Any, ...]]:
def first_valid_index(self) -> Optional[Union[Any, Tuple[Any, ...]]]:
"""
Retrieves the index of the first valid value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we modify Returns in docstrings by including None?


def last_valid_index(self) -> Union[Any, Tuple[Any, ...]]:
def last_valid_index(self) -> Optional[Union[Any, Tuple[Any, ...]]]:
"""
Return index for last non-NA/null value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

@@ -1863,25 +1868,32 @@ def last_valid_index(self) -> Union[Any, Tuple[Any, ...]]:
>>> s.last_valid_index() # doctest: +SKIP
('cow', 'weight')
"""
sdf = self._internal.spark_frame
if LooseVersion(pyspark.__version__) < LooseVersion("3.0"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's better than specifying in docstrings only!

@xinrong-meng
Copy link
Contributor

Other than two docstring improvements, LGTM!

@xinrong-meng xinrong-meng self-requested a review November 22, 2020 21:54
@ueshin
Copy link
Collaborator Author

ueshin commented Nov 23, 2020

Thanks! merging.

@ueshin ueshin merged commit c55b634 into databricks:master Nov 23, 2020
@ueshin ueshin deleted the first_last_valid_index branch November 23, 2020 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants