Skip to content

Conversation

jrycw
Copy link
Collaborator

@jrycw jrycw commented Mar 2, 2025

Hello team,

This PR introduces cols_reorder(), allowing users to reorder columns in a specified order easily.

While the existing move_*() functions are useful for repositioning a few columns, they don't provide fine-grained control over column order. I've often had to rely on Pandas or Polars for this task. The implementation of this function follows a straightforward approach, similar to the existing move_*() functions.

Feedback is always welcome!😊

Copy link

codecov bot commented Mar 2, 2025

Codecov Report

Attention: Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.86%. Comparing base (dc82197) to head (60d43ce).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
great_tables/_spanners.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #625      +/-   ##
==========================================
+ Coverage   90.85%   90.86%   +0.01%     
==========================================
  Files          46       46              
  Lines        5423     5432       +9     
==========================================
+ Hits         4927     4936       +9     
  Misses        496      496              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

function: `cols_reorder()`. By passing a list of column names to the `columns=` parameter,
the table will be reordered accordingly:
```{python}
col1, col2, col3, *cols = exibble.columns # col1="num", col2="char", col3="fctr"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good example!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed the variables to use their actual column names instead of generic names like col1. This should improve readability.


if isinstance(columns, str):
columns = [columns]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should raise here, if only to provide a more user-friendly message. The user will instead see "Reordering vars must contain all boxhead vars." if not providing all columns, which is good for a developer-facing error message but it exposes some of the underlying implementation that the user might not know about.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Should we raise the exception inside the function or modify the error message to be more user-friendly rather than developer-focused?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I’m for slightly tweaking the wording in the existing location. Just switch vars with columns and avoid the use of boxhead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Column reordering must include all columns in the table."?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants