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-#2735: move '.reindex' logic about axis dispatching from the base… #2736

Merged
merged 1 commit into from
Feb 15, 2021
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
64 changes: 11 additions & 53 deletions modin/pandas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,65 +1686,28 @@ def rank(

def reindex(
self,
labels=None,
index=None,
columns=None,
axis=None,
method=None,
copy=True,
level=None,
fill_value=np.nan,
limit=None,
tolerance=None,
**kwargs,
):
axis = self._get_axis_number(axis)
if (columns is not None and self._query_compiler.has_multiindex(axis=1)) or (
index is not None and self._query_compiler.has_multiindex()
):
return self._default_to_pandas(
"reindex",
labels=labels,
index=index,
columns=columns,
method=method,
copy=copy,
level=level,
fill_value=fill_value,
limit=limit,
tolerance=tolerance,
)
if (
level is not None
or (axis == 1 and self._query_compiler.has_multiindex(axis=1))
or (axis == 0 and self._query_compiler.has_multiindex())
kwargs.get("level") is not None
or (index is not None and self._query_compiler.has_multiindex())
or (columns is not None and self._query_compiler.has_multiindex(axis=1))
):
return self._default_to_pandas(
"reindex",
labels=labels,
level=level,
method=method,
copy=copy,
axis=axis,
fill_value=fill_value,
limit=limit,
tolerance=tolerance,
)
if axis == 0 and labels is not None:
index = labels
elif labels is not None:
columns = labels
if index is not None:
kwargs["index"] = index
if columns is not None:
kwargs["columns"] = columns
return self._default_to_pandas("reindex", copy=copy, **kwargs)
new_query_compiler = None
if index is not None:
if not isinstance(index, pandas.Index):
index = pandas.Index(index)
if not index.equals(self.index):
new_query_compiler = self._query_compiler.reindex(
axis=0,
labels=index,
method=method,
fill_value=fill_value,
limit=limit,
tolerance=tolerance,
axis=0, labels=index, **kwargs
)
if new_query_compiler is None:
new_query_compiler = self._query_compiler
Expand All @@ -1754,12 +1717,7 @@ def reindex(
columns = pandas.Index(columns)
if not columns.equals(self.columns):
final_query_compiler = new_query_compiler.reindex(
axis=1,
labels=columns,
method=method,
fill_value=fill_value,
limit=limit,
tolerance=tolerance,
axis=1, labels=columns, **kwargs
)
if final_query_compiler is None:
final_query_compiler = new_query_compiler
Expand Down
29 changes: 29 additions & 0 deletions modin/pandas/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,35 @@ def query(self, expr, inplace=False, **kwargs):
new_query_compiler = self._query_compiler.query(expr, **kwargs)
return self._create_or_update_from_compiler(new_query_compiler, inplace)

def reindex(
self,
labels=None,
index=None,
columns=None,
axis=None,
method=None,
copy=True,
level=None,
fill_value=np.nan,
limit=None,
tolerance=None,
):
axis = self._get_axis_number(axis)
if axis == 0 and labels is not None:
index = labels
elif labels is not None:
columns = labels
return super(DataFrame, self).reindex(
index=index,
columns=columns,
method=method,
copy=copy,
level=level,
fill_value=fill_value,
limit=limit,
tolerance=tolerance,
)

def rename(
self,
mapper=None,
Expand Down
9 changes: 9 additions & 0 deletions modin/pandas/test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,15 @@ def test_reindex(data):
pandas_series.reindex(index=[0, 1, 5]),
)

# MultiIndex
modin_series, pandas_series = create_test_series(data)
modin_series.index, pandas_series.index = [
generate_multiindex(len(pandas_series))
] * 2
pandas_result = pandas_series.reindex(list(reversed(pandas_series.index)))
modin_result = modin_series.reindex(list(reversed(modin_series.index)))
df_equals(pandas_result, modin_result)


def test_reindex_like():
df1 = pd.DataFrame(
Expand Down