westonpace commented on code in PR #13493:
URL: https://github.com/apache/arrow/pull/13493#discussion_r920447579
##########
python/pyarrow/table.pxi:
##########
@@ -4631,6 +4631,89 @@ cdef class Table(_PandasConvertible):
return table
+ def group_by(self, keys):
+ """Declare a grouping over the columns of the table.
+
+ Resulting grouping can then be used to perform aggregations
+ with a subsequent ``aggregate()`` method.
+
+ Parameters
+ ----------
+ keys : str or list[str]
+ Name of the columns that should be used as the grouping key.
+
+ Returns
+ -------
+ TableGroupBy
+
+ See Also
+ --------
+ TableGroupBy.aggregate
+
+ Examples
+ --------
+ >>> import pandas as pd
+ >>> import pyarrow as pa
+ >>> df = pd.DataFrame({'year': [2020, 2022, 2021, 2022, 2019, 2021],
+ ... 'n_legs': [2, 2, 4, 4, 5, 100],
+ ... 'animal': ["Flamingo", "Parrot", "Dog", "Horse",
+ ... "Brittle stars", "Centipede"]})
+ >>> table = pa.Table.from_pandas(df)
+ >>> table.group_by('year').aggregate([('n_legs', 'sum')])
+ pyarrow.Table
+ n_legs_sum: int64
+ year: int64
+ ----
+ n_legs_sum: [[2,6,104,5]]
+ year: [[2020,2022,2021,2019]]
+ """
+ return TableGroupBy(self, keys)
+
+ def sort_by(self, sorting):
+ """
+ Sort the table by one or multiple columns.
+
+ Parameters
+ ----------
+ sorting : str or list[tuple(name, order)]
+ Name of the column to use to sort (ascending), or
+ a list of multiple sorting conditions where
+ each entry is a tuple with column name
+ and sorting order ("ascending" or "descending")
+
+ Returns
+ -------
+ Table
+ A new table sorted according to the sort keys.
+
+ Examples
+ --------
+ >>> import pandas as pd
+ >>> import pyarrow as pa
+ >>> df = pd.DataFrame({'year': [2020, 2022, 2021, 2022, 2019, 2021],
+ ... 'n_legs': [2, 2, 4, 4, 5, 100],
+ ... 'animal': ["Flamingo", "Parrot", "Dog", "Horse",
+ ... "Brittle stars", "Centipede"]})
+ >>> table = pa.Table.from_pandas(df)
+ >>> table.sort_by('animal')
+ pyarrow.Table
+ year: int64
+ n_legs: int64
+ animal: string
+ ----
+ year: [[2019,2021,2021,2020,2022,2022]]
+ n_legs: [[5,100,4,2,4,2]]
+ animal: [["Brittle
stars","Centipede","Dog","Flamingo","Horse","Parrot"]]
+ """
+ if isinstance(sorting, str):
+ sorting = [(sorting, "ascending")]
+
+ indices = _pc().sort_indices(
+ self,
+ sort_keys=sorting
+ )
+ return self.take(indices)
+
Review Comment:
In this case it's not needed (the function only has to have been defined
before it is actually called and all functions will be defined when the file is
imported so we should be fine).
However, I think it's fine to leave this change in. It can be a slight
boost for readability to have functions declared in order and I don't think
there are any active PRs in this area right now that will need to rebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]