Skip to content

Commit

Permalink
FIX-#2365: Fix Series.value_counts when dropna=False (#2366)
Browse files Browse the repository at this point in the history
Signed-off-by: Igoshev, Yaroslav <yaroslav.igoshev@intel.com>
  • Loading branch information
YarShev committed Nov 5, 2020
1 parent a3e06c7 commit f4f3a1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
19 changes: 11 additions & 8 deletions modin/backends/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,18 +732,21 @@ def reduce_func(df, *args, **kwargs):
dropna = kwargs.get("dropna", True)

try:
result = df.squeeze(axis=1).groupby(df.index, sort=False).sum()
result = (
df.squeeze(axis=1)
.groupby(df.index, sort=False, dropna=dropna)
.sum()
)
# This will happen with Arrow buffer read-only errors. We don't want to copy
# all the time, so this will try to fast-path the code first.
except (ValueError):
result = df.copy().squeeze(axis=1).groupby(df.index, sort=False).sum()

if not dropna and np.nan in df.index:
result = result.append(
pandas.Series(
[df.squeeze(axis=1).loc[[np.nan]].sum()], index=[np.nan]
)
result = (
df.copy()
.squeeze(axis=1)
.groupby(df.index, sort=False, dropna=dropna)
.sum()
)

if normalize:
result = result / df.squeeze(axis=1).sum()

Expand Down
20 changes: 20 additions & 0 deletions modin/pandas/test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,26 @@ def sort_index_for_equal_values(result, ascending):
)
df_equals(modin_result, pandas_result)

# from issue #2365
arr = np.random.rand(2 ** 6)
arr[::10] = np.nan
modin_series, pandas_series = create_test_series(arr)
modin_result = modin_series.value_counts(dropna=False, ascending=True)
pandas_result = sort_index_for_equal_values(
pandas_series.value_counts(dropna=False, ascending=True), True
)
if get_current_backend() == "BaseOnPython":
modin_result = sort_index_for_equal_values(modin_result, ascending=True)
df_equals(modin_result, pandas_result)

modin_result = modin_series.value_counts(dropna=False, ascending=False)
pandas_result = sort_index_for_equal_values(
pandas_series.value_counts(dropna=False, ascending=False), False
)
if get_current_backend() == "BaseOnPython":
modin_result = sort_index_for_equal_values(modin_result, ascending=False)
df_equals(modin_result, pandas_result)


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_values(data):
Expand Down

0 comments on commit f4f3a1e

Please sign in to comment.