Skip to content

[DataFrame] Implement diff #1996

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

Merged
merged 8 commits into from
May 5, 2018
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
30 changes: 26 additions & 4 deletions python/ray/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,9 +1356,31 @@ def describe_helper(df):
return result

def diff(self, periods=1, axis=0):
raise NotImplementedError(
"To contribute to Pandas on Ray, please visit "
"github.com/ray-project/ray.")
"""Finds the difference between elements on the axis requested

Args:
periods: Periods to shift for forming difference
axis: Take difference over rows or columns

Returns:
DataFrame with the diff applied
"""
axis = pd.DataFrame()._get_axis_number(axis)
partitions = (self._col_partitions if
axis == 0 else self._row_partitions)

result = _map_partitions(lambda df:
df.diff(axis=axis, periods=periods),
partitions)

if (axis == 1):
return DataFrame(row_partitions=result,
columns=self.columns,
index=self.index)
if (axis == 0):
return DataFrame(col_partitions=result,
columns=self.columns,
index=self.index)

def div(self, other, axis='columns', level=None, fill_value=None):
"""Divides this DataFrame against another DataFrame/Series/scalar.
Expand Down Expand Up @@ -1573,7 +1595,7 @@ def helper(df, index, other_series):
# TODO: group series here into full df partitions to reduce
# the number of remote calls to helper
other_series = other_df.iloc[idx['index_within_partition']]
curr_index = self._row_metadata._coord_df.iloc[i]
curr_index = self._row_metadata._coord_df.loc[i]
curr_df = self._row_partitions[int(curr_index['partition'])]
results.append(_deploy_func.remote(helper,
curr_df,
Expand Down
14 changes: 9 additions & 5 deletions python/ray/dataframe/test/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def test_int_dataframe():
test_quantile(ray_df, pandas_df, .5)
test_quantile(ray_df, pandas_df, .75)
test_describe(ray_df, pandas_df)
test_diff(ray_df, pandas_df)

test_all(ray_df, pandas_df)
test_any(ray_df, pandas_df)
Expand Down Expand Up @@ -380,6 +381,7 @@ def test_float_dataframe():
test_quantile(ray_df, pandas_df, .5)
test_quantile(ray_df, pandas_df, .75)
test_describe(ray_df, pandas_df)
test_diff(ray_df, pandas_df)

test_all(ray_df, pandas_df)
test_any(ray_df, pandas_df)
Expand Down Expand Up @@ -695,6 +697,7 @@ def test_nan_dataframe():
test_quantile(ray_df, pandas_df, .5)
test_quantile(ray_df, pandas_df, .75)
test_describe(ray_df, pandas_df)
test_diff(ray_df, pandas_df)

test_all(ray_df, pandas_df)
test_any(ray_df, pandas_df)
Expand Down Expand Up @@ -1133,11 +1136,12 @@ def test_describe(ray_df, pandas_df):
assert(ray_df.describe().equals(pandas_df.describe()))


def test_diff():
ray_df = create_test_dataframe()

with pytest.raises(NotImplementedError):
ray_df.diff()
@pytest.fixture
def test_diff(ray_df, pandas_df):
Copy link
Contributor

Choose a reason for hiding this comment

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

Try different axis and period values in the tests.

assert(ray_df_equals_pandas(ray_df.diff(), pandas_df.diff()))
assert(ray_df_equals_pandas(ray_df.diff(axis=1), pandas_df.diff(axis=1)))
assert(ray_df_equals_pandas(ray_df.diff(periods=1),
pandas_df.diff(periods=1)))


def test_div():
Expand Down