Skip to content

Commit

Permalink
FIX-#2735: move '.reindex' logic about axis dispatching from the base…
Browse files Browse the repository at this point in the history
… class

Signed-off-by: Dmitry Chigarev <[email protected]>
  • Loading branch information
dchigarev committed Feb 15, 2021
1 parent 33a57e3 commit dfa1c23
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
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

0 comments on commit dfa1c23

Please sign in to comment.