Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 8 additions & 13 deletions databricks/koalas/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6448,14 +6448,12 @@ def swaplevel(self, i=-2, j=-1, axis=0) -> "DataFrame":

return DataFrame(internal)

def swapaxes(
self, i: Union[str, int] = 0, j: Union[str, int] = 1, copy: bool = True
) -> "DataFrame":
def swapaxes(self, i: Union[str, int], j: Union[str, int], copy: bool = True) -> "DataFrame":
"""
Interchange axes and swap values axes appropriately.

.. note:: This method is based on an expensive operation due to the nature
of big data. Internally it needs to generate each row for each value, and
.. note:: This method, if applied to a DataFrame, is based on an expensive operation due to
the nature of big data. Internally it needs to generate each row for each value, and
then group twice - it is a huge operation. To prevent misusage, this method
has the 'compute.max_rows' default limit of input length, and raises a ValueError.

Expand All @@ -6471,8 +6469,9 @@ def swapaxes(

Parameters
----------
i: {0 or 'index', 1 or 'columns'}, default 0. The axis to swap.
j: {0 or 'index', 1 or 'columns'}, default 1. The axis to swap.
i: {0 or 'index', 1 or 'columns'}. The axis to swap.
j: {0 or 'index', 1 or 'columns'}. The axis to swap.
copy : bool, default True.

Returns
-------
Expand All @@ -6488,11 +6487,6 @@ def swapaxes(
x 1 2 3
y 4 5 6
z 7 8 9
>>> kdf.swapaxes()
x y z
a 1 4 7
b 2 5 8
c 3 6 9
>>> kdf.swapaxes(i=1, j=0)
x y z
a 1 4 7
Expand All @@ -6505,10 +6499,11 @@ def swapaxes(
z 7 8 9
"""
assert copy is True

i = validate_axis(i)
j = validate_axis(j)

return self if i == j else self.transpose()
return self.copy() if i == j else self.transpose()

def _swaplevel_columns(self, i, j) -> InternalFrame:
assert isinstance(self.columns, pd.MultiIndex)
Expand Down
1 change: 0 additions & 1 deletion databricks/koalas/missing/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class MissingPandasLikeSeries(object):
sem = _unsupported_function("sem")
set_axis = _unsupported_function("set_axis")
slice_shift = _unsupported_function("slice_shift")
swapaxes = _unsupported_function("swapaxes")
to_hdf = _unsupported_function("to_hdf")
to_period = _unsupported_function("to_period")
to_sql = _unsupported_function("to_sql")
Expand Down
40 changes: 40 additions & 0 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,46 @@ def swaplevel(self, i=-2, j=-1, copy: bool = True) -> "Series":

return first_series(self.to_frame().swaplevel(i, j, axis=0)).rename(self.name)

def swapaxes(self, i: Union[str, int], j: Union[str, int], copy: bool = True) -> "Series":
"""
Interchange axes and swap values axes appropriately.

Parameters
----------
i: {0 or 'index', 1 or 'columns'}. The axis to swap.
j: {0 or 'index', 1 or 'columns'}. The axis to swap.
copy : bool, default True.

Returns
-------
Series

Examples
--------
On a Series:

>>> kser = ks.Series([1, 2, 3], index=["x", "y", "z"])
>>> kser
x 1
y 2
z 3
dtype: int64
>>>
>>> kser.swapaxes(0, 0)
x 1
y 2
z 3
dtype: int64
"""
assert copy is True

i = validate_axis(i)
j = validate_axis(j)
if not i == j == 0:
raise ValueError("Axis must be 0 for Series")

return self.copy()

def add_prefix(self, prefix) -> "Series":
"""
Prefix labels with string `prefix`.
Expand Down
1 change: 1 addition & 0 deletions databricks/koalas/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ def test_swapaxes(self):

self.assertRaises(AssertionError, lambda: kdf.swapaxes(0, 1, copy=False))
self.assertRaises(ValueError, lambda: kdf.swapaxes(0, -1))
self.assertRaises(TypeError, lambda: kdf.swapaxes())

def test_nlargest(self):
pdf = pd.DataFrame(
Expand Down
13 changes: 13 additions & 0 deletions databricks/koalas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,19 @@ def test_swaplevel(self):
self.assertRaises(KeyError, lambda: kser.swaplevel("not_number", "color"))
self.assertRaises(AssertionError, lambda: kser.swaplevel(copy=False))

def test_swapaxes(self):
pser = pd.Series([1, 2, 3], index=["x", "y", "z"], name="ser")
kser = ks.from_pandas(pser)

self.assert_eq(kser.swapaxes(0, 0), pser.swapaxes(0, 0))
self.assert_eq(kser.swapaxes("index", "index"), pser.swapaxes("index", "index"))
self.assert_eq((kser + 1).swapaxes(0, 0), (pser + 1).swapaxes(0, 0))

self.assertRaises(AssertionError, lambda: kser.swapaxes(0, 1, copy=False))
self.assertRaises(ValueError, lambda: kser.swapaxes(0, 1))
self.assertRaises(ValueError, lambda: kser.swapaxes("index", "columns"))
self.assertRaises(TypeError, lambda: kser.swapaxes())

def test_div_zero_and_nan(self):
pser = pd.Series([100, None, -300, None, 500, -700, np.inf, -np.inf], name="Koalas")
kser = ks.from_pandas(pser)
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Reindexing / Selection / Label manipulation
Series.reset_index
Series.sample
Series.swaplevel
Series.swapaxes
Series.take
Series.tail
Series.where
Expand Down