Skip to content

Commit 275df31

Browse files
feat: Add df.sort_index(axis=1)
1 parent f9e28fe commit 275df31

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

bigframes/dataframe.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,25 +2555,33 @@ def sort_index(
25552555
) -> None:
25562556
...
25572557

2558-
@validations.requires_index
25592558
def sort_index(
25602559
self,
25612560
*,
2561+
axis: Union[int, str] = 0,
25622562
ascending: bool = True,
25632563
inplace: bool = False,
25642564
na_position: Literal["first", "last"] = "last",
25652565
) -> Optional[DataFrame]:
2566-
if na_position not in ["first", "last"]:
2567-
raise ValueError("Param na_position must be one of 'first' or 'last'")
2568-
na_last = na_position == "last"
2569-
index_columns = self._block.index_columns
2570-
ordering = [
2571-
order.ascending_over(column, na_last)
2572-
if ascending
2573-
else order.descending_over(column, na_last)
2574-
for column in index_columns
2575-
]
2576-
block = self._block.order_by(ordering)
2566+
if utils.get_axis_number(axis) == 0:
2567+
if na_position not in ["first", "last"]:
2568+
raise ValueError("Param na_position must be one of 'first' or 'last'")
2569+
na_last = na_position == "last"
2570+
index_columns = self._block.index_columns
2571+
ordering = [
2572+
order.ascending_over(column, na_last)
2573+
if ascending
2574+
else order.descending_over(column, na_last)
2575+
for column in index_columns
2576+
]
2577+
block = self._block.order_by(ordering)
2578+
else: # axis=1
2579+
_, indexer = self.columns.sort_values(
2580+
return_indexer=True, ascending=ascending, na_position=na_position # type: ignore
2581+
)
2582+
block = self._block.select_columns(
2583+
[self._block.value_columns[i] for i in indexer]
2584+
)
25772585
if inplace:
25782586
self._set_block(block)
25792587
return None

tests/system/small/test_dataframe.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,13 +2406,19 @@ def test_set_index_key_error(scalars_dfs):
24062406
("na_position",),
24072407
(("first",), ("last",)),
24082408
)
2409-
def test_sort_index(scalars_dfs, ascending, na_position):
2409+
@pytest.mark.parametrize(
2410+
("axis",),
2411+
((0,), ("columns",)),
2412+
)
2413+
def test_sort_index(scalars_dfs, ascending, na_position, axis):
24102414
index_column = "int64_col"
24112415
scalars_df, scalars_pandas_df = scalars_dfs
24122416
df = scalars_df.set_index(index_column)
2413-
bf_result = df.sort_index(ascending=ascending, na_position=na_position).to_pandas()
2417+
bf_result = df.sort_index(
2418+
ascending=ascending, na_position=na_position, axis=axis
2419+
).to_pandas()
24142420
pd_result = scalars_pandas_df.set_index(index_column).sort_index(
2415-
ascending=ascending, na_position=na_position
2421+
ascending=ascending, na_position=na_position, axis=axis
24162422
)
24172423
pandas.testing.assert_frame_equal(bf_result, pd_result)
24182424

tests/unit/test_dataframe_polars.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,13 +1757,19 @@ def test_set_index_key_error(scalars_dfs):
17571757
("na_position",),
17581758
(("first",), ("last",)),
17591759
)
1760-
def test_sort_index(scalars_dfs, ascending, na_position):
1760+
@pytest.mark.parametrize(
1761+
("axis",),
1762+
((0,), ("columns",)),
1763+
)
1764+
def test_sort_index(scalars_dfs, ascending, na_position, axis):
17611765
index_column = "int64_col"
17621766
scalars_df, scalars_pandas_df = scalars_dfs
17631767
df = scalars_df.set_index(index_column)
1764-
bf_result = df.sort_index(ascending=ascending, na_position=na_position).to_pandas()
1768+
bf_result = df.sort_index(
1769+
ascending=ascending, na_position=na_position, axis=axis
1770+
).to_pandas()
17651771
pd_result = scalars_pandas_df.set_index(index_column).sort_index(
1766-
ascending=ascending, na_position=na_position
1772+
ascending=ascending, na_position=na_position, axis=axis
17671773
)
17681774
pandas.testing.assert_frame_equal(bf_result, pd_result)
17691775

third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,13 +2382,17 @@ def sort_values(
23822382
def sort_index(
23832383
self,
23842384
*,
2385+
axis: str | int = 0,
23852386
ascending: bool = True,
23862387
inplace: bool = False,
23872388
na_position: Literal["first", "last"] = "last",
23882389
):
23892390
"""Sort object by labels (along an axis).
23902391
23912392
Args:
2393+
axis ({0 or 'index', 1 or 'columns'}, default 0):
2394+
The axis along which to sort. The value 0 identifies the rows,
2395+
and 1 identifies the columns.
23922396
ascending (bool, default True)
23932397
Sort ascending vs. descending.
23942398
inplace (bool, default False):

0 commit comments

Comments
 (0)