posit-dev / posit-dev/py-shiny

More Flexibility with Data Table Sorting Order with np.nan

Open
#1,833 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

When having a numeric column in a dataframe, the default for sorting seems to be to have negative values sorted, then np.nan or none values, then positive values. I get this is default behavior in pandas, but it makes the dataframes a bit cumbersome to view when I have some columns with a lot of blank values but also negative and positive values (big gap between my populated data). However, I don't want to filter the entire row out, since other columns of that row do have valid values.

The update_sort you added allows for sorting by multiple columns with buttons, but doesn't change the underlying behavior of the sort.

Is there any way to add an argument, such as na_position=last, so that blank values can go to the end?

Here is a basic example to illustrate what I am asking:

from shiny import App, render, ui
import pandas as pd
import numpy as np
data = {
    'ID': range(1, 11),
    'Name': ['Item ' + str(i) for i in range(1, 11)],
    'Value': [10.5, -5.2, np.nan, 15.7, -8.3, np.nan, 22.1, -12.4, 7.8, -3.9]
}
df = pd.DataFrame(data)

app_ui = ui.page_fluid(
    ui.h2("Data Grid Example"),
    ui.card(
        ui.output_data_frame("grid")
    )
)

def server(input, output, session):
    @output
    @render.data_frame
    def grid():
        return render.DataGrid(df)

app = App(app_ui, server) 

In this example, the sort will look like this:

Image

However, ideally, I'd like the na_position=last option so the blanks would be at the end

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with render.DataGrid and the existing update_sort behavior described in the issue, then trace where dataframe column sorting is handled. Add a way to choose the position of missing values, such as na_position=last, and verify that rows remain intact while blank values move to the requested position.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data, frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.