Skip to content

Commit

Permalink
REFACTOR-modin-project#5092: Fix future warning for set_axis functi…
Browse files Browse the repository at this point in the history
…on (modin-project#5093)

Co-authored-by: Vasily Litvinov <fam1ly.n4me@yandex.ru>
Signed-off-by: Myachev <anatoly.myachev@intel.com>
  • Loading branch information
2 people authored and RehanSD committed Oct 10, 2022
1 parent f988e35 commit f30cbc0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
2 changes: 2 additions & 0 deletions modin/_compat/pandas_api/latest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ def sample(
)

def set_axis(self, labels, axis=0, inplace=no_default, *, copy=no_default):
if inplace is True and copy is True:
raise ValueError("Cannot specify both inplace=True and copy=True")
return self._set_axis(
labels=labels,
axis=axis,
Expand Down
8 changes: 3 additions & 5 deletions modin/core/dataframe/pandas/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,7 @@ def _propagate_index_objs(self, axis=None):
if axis is None:

def apply_idx_objs(df, idx, cols):
return df.set_axis(idx, axis="index", inplace=False).set_axis(
cols, axis="columns", inplace=False
)
return df.set_axis(idx, axis="index").set_axis(cols, axis="columns")

self._partitions = np.array(
[
Expand All @@ -576,7 +574,7 @@ def apply_idx_objs(df, idx, cols):
elif axis == 0:

def apply_idx_objs(df, idx):
return df.set_axis(idx, axis="index", inplace=False)
return df.set_axis(idx, axis="index")

self._partitions = np.array(
[
Expand All @@ -598,7 +596,7 @@ def apply_idx_objs(df, idx):
elif axis == 1:

def apply_idx_objs(df, cols):
return df.set_axis(cols, axis="columns", inplace=False)
return df.set_axis(cols, axis="columns")

self._partitions = np.array(
[
Expand Down
12 changes: 6 additions & 6 deletions modin/core/storage_formats/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,9 @@ def infer_objects(self):
def first_valid_index(self):
def first_valid_index_builder(df):
"""Get the position of the first valid index in a single partition."""
return df.set_axis(
pandas.RangeIndex(len(df.index)), axis="index", inplace=False
).apply(lambda df: df.first_valid_index())
return df.set_axis(pandas.RangeIndex(len(df.index)), axis="index").apply(
lambda df: df.first_valid_index()
)

# We get the minimum from each column, then take the min of that to get
# first_valid_index. The `to_pandas()` here is just for a single value and
Expand All @@ -1562,9 +1562,9 @@ def first_valid_index_builder(df):
def last_valid_index(self):
def last_valid_index_builder(df):
"""Get the position of the last valid index in a single partition."""
return df.set_axis(
pandas.RangeIndex(len(df.index)), axis="index", inplace=False
).apply(lambda df: df.last_valid_index())
return df.set_axis(pandas.RangeIndex(len(df.index)), axis="index").apply(
lambda df: df.last_valid_index()
)

# We get the maximum from each column, then take the max of that to get
# last_valid_index. The `to_pandas()` here is just for a single value and
Expand Down
20 changes: 8 additions & 12 deletions modin/pandas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,7 @@ def reorder_levels(self, order, axis=0): # noqa: PR01, RT01, D200
"""
axis = self._get_axis_number(axis)
new_labels = self.axes[axis].reorder_levels(order)
return self.set_axis(new_labels, axis=axis, inplace=False)
return self.set_axis(new_labels, axis=axis)

def _resample(
self,
Expand Down Expand Up @@ -2533,13 +2533,9 @@ def _set_axis(self, labels, axis, inplace, copy): # noqa: PR01, RT01, D200
stacklevel=2,
)
labels, axis = axis, labels
if inplace:
setattr(self, pandas.DataFrame()._get_axis_name(axis), labels)
else:
if copy:
obj = self.copy()
obj.set_axis(labels, axis=axis, inplace=True)
return obj
obj = self.copy() if copy else self
setattr(obj, pandas.DataFrame._get_axis_name(axis), labels)
return None if inplace else obj

def _shift(self, periods, freq, axis, fill_value): # noqa: PR01, RT01, D200
"""
Expand Down Expand Up @@ -2745,7 +2741,7 @@ def swaplevel(self, i=-2, j=-1, axis=0): # noqa: PR01, RT01, D200
"""
axis = self._get_axis_number(axis)
idx = self.index if axis == 0 else self.columns
return self.set_axis(idx.swaplevel(i, j), axis=axis, inplace=False)
return self.set_axis(idx.swaplevel(i, j), axis=axis)

def tail(self, n=5): # noqa: PR01, RT01, D200
"""
Expand Down Expand Up @@ -2940,7 +2936,7 @@ def tshift(self, periods=1, freq=None, axis=0): # noqa: PR01, RT01, D200
"""
axis = self._get_axis_number(axis)
new_labels = self.axes[axis].shift(periods, freq=freq)
return self.set_axis(new_labels, axis=axis, inplace=False)
return self.set_axis(new_labels, axis=axis)

def transform(self, func, axis=0, *args, **kwargs): # noqa: PR01, RT01, D200
"""
Expand Down Expand Up @@ -2972,7 +2968,7 @@ def tz_convert(self, tz, axis=0, level=None, copy=True): # noqa: PR01, RT01, D2
else:
new_labels = self.axes[axis].tz_convert(tz)
obj = self.copy() if copy else self
return obj.set_axis(new_labels, axis, inplace=not copy)
return obj._set_axis(new_labels, axis, inplace=False, copy=copy)

def tz_localize(
self, tz, axis=0, level=None, copy=True, ambiguous="raise", nonexistent="raise"
Expand All @@ -2993,7 +2989,7 @@ def tz_localize(
)
.index
)
return self.set_axis(labels=new_labels, axis=axis, inplace=not copy)
return self._set_axis(new_labels, axis, inplace=False, copy=copy)

# TODO: uncomment the following lines when #3331 issue will be closed
# @prepend_to_notes(
Expand Down
13 changes: 9 additions & 4 deletions modin/pandas/test/dataframe/test_join_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,19 @@ def test_sort_index(axis, ascending, na_position):
for df in [modin_df, pandas_df]:
df.index = [(i - length / 2) % length for i in range(length)]

dfs = [modin_df, pandas_df]
kw = {"copy": False}
if PandasCompatVersion.CURRENT == PandasCompatVersion.PY36:
kw = {"inplace": False}
# Add NaNs to sorted index
for df in [modin_df, pandas_df]:
sort_index = df.axes[axis]
df.set_axis(
for idx in range(len(dfs)):
sort_index = dfs[idx].axes[axis]
dfs[idx] = dfs[idx].set_axis(
[np.nan if i % 2 == 0 else sort_index[i] for i in range(len(sort_index))],
axis=axis,
inplace=True,
**kw,
)
modin_df, pandas_df = dfs

eval_general(
modin_df,
Expand Down
13 changes: 8 additions & 5 deletions modin/pandas/test/dataframe/test_map_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,15 @@ def test_set_axis(data, axis):
index = modin_df.columns if x else modin_df.index
labels = ["{0}_{1}".format(index[i], i) for i in range(modin_df.shape[x])]

modin_result = modin_df.set_axis(labels, axis=axis, inplace=False)
pandas_result = pandas_df.set_axis(labels, axis=axis, inplace=False)
df_equals(modin_result, pandas_result)
kw = {"copy": True}
if PandasCompatVersion.CURRENT == PandasCompatVersion.PY36:
kw = {"inplace": False}
eval_general(modin_df, pandas_df, lambda df: df.set_axis(labels, axis=axis, **kw))

modin_df_copy = modin_df.copy()
modin_df.set_axis(labels, axis=axis, inplace=True)
if "copy" in kw:
kw["copy"] = False
modin_df = modin_df.set_axis(labels, axis=axis, **kw)

# Check that the copy and original are different
try:
Expand All @@ -1232,7 +1235,7 @@ def test_set_axis(data, axis):
else:
assert False

pandas_df.set_axis(labels, axis=axis, inplace=True)
pandas_df = pandas_df.set_axis(labels, axis=axis, **kw)
df_equals(modin_df, pandas_df)


Expand Down

0 comments on commit f30cbc0

Please sign in to comment.