-
Notifications
You must be signed in to change notification settings - Fork 358
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
Add value_counts. #63
Merged
+53
−3
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
import numpy as np | ||
import pyspark.sql.functions as F | ||
from pyspark.sql import DataFrame, Column | ||
from pyspark.sql.types import StructType, to_arrow_type | ||
from pyspark.sql.types import FloatType, DoubleType, StructType, to_arrow_type | ||
from pyspark.sql.utils import AnalysisException | ||
|
||
from . import namespace | ||
|
@@ -260,6 +260,17 @@ def to_dataframe(self): | |
def toPandas(self): | ||
return _col(self.to_dataframe().toPandas()) | ||
|
||
def isna(self): | ||
if isinstance(self.schema[self.name].dataType, (FloatType, DoubleType)): | ||
return self.isNull() | F.isnan(self) | ||
else: | ||
return self.isNull() | ||
|
||
isnull = isna | ||
|
||
def notna(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add a doc here when working on #64. |
||
return ~self.isna() | ||
|
||
@derived_from(pd.Series) | ||
def dropna(self, axis=0, inplace=False, **kwargs): | ||
col = _col(self.to_dataframe().dropna(axis=axis, inplace=False)) | ||
|
@@ -278,6 +289,28 @@ def unique(self): | |
# Pandas wants a series/array-like object | ||
return _col(self.to_dataframe().unique()) | ||
|
||
@derived_from(pd.Series) | ||
def value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True): | ||
if bins is not None: | ||
raise NotImplementedError("value_counts currently does not support bins") | ||
|
||
if dropna: | ||
df_dropna = self.to_dataframe()._spark_filter(self.notna()) | ||
else: | ||
df_dropna = self.to_dataframe() | ||
df = df_dropna._spark_groupby(self).count() | ||
if sort: | ||
if ascending: | ||
df = df._spark_orderBy(F._spark_col('count')) | ||
else: | ||
df = df._spark_orderBy(F._spark_col('count')._spark_desc()) | ||
|
||
if normalize: | ||
sum = df_dropna._spark_count() | ||
df = df._spark_withColumn('count', F._spark_col('count') / F._spark_lit(sum)) | ||
|
||
return _col(df.set_index([self.name])) | ||
|
||
@property | ||
def _pandas_anchor(self) -> DataFrame: | ||
""" | ||
|
@@ -535,8 +568,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None, inplace=False): | |
columns = list(self.columns) | ||
|
||
cnt = reduce(lambda x, y: x + y, | ||
[F._spark_when(F._spark_col(column)._spark_isNotNull(), 1) | ||
._spark_otherwise(0) | ||
[F._spark_when(self[column].notna(), 1)._spark_otherwise(0) | ||
for column in columns], | ||
F._spark_lit(0)) | ||
if thresh is not None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto.