Skip to content

dataframe

Functions for processing dataframes for the Table views.

df_columns(df) #

Return a list of column names from a dataframe.

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
29
30
31
32
33
34
35
36
37
38
def df_columns(df) -> List[str]:
    """Return a list of column names from a dataframe."""
    if df_type(df) == "vaex":
        return df.get_column_names()
    elif df_type(df) == "pandas":
        return df.columns.tolist()
    elif df_type(df) == "polars":
        return df.columns
    else:
        raise TypeError(f"{type(df)} not supported")

df_len(df) #

Return the number of rows in a dataframe.

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
24
25
26
def df_len(df) -> int:
    """Return the number of rows in a dataframe."""
    return len(df)

df_records(df) #

A list of records from a dataframe.

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
59
60
61
62
63
64
65
66
67
68
def df_records(df) -> List[dict]:
    """A list of records from a dataframe."""
    if df_type(df) == "pandas":
        return df.to_dict("records")
    elif df_type(df) == "polars":
        return df.to_dicts()
    elif df_type(df) == "vaex":
        return df.to_records()
    else:
        raise TypeError(f"{type(df)} not supported")

df_row_names(df) #

Return a list of row names from a dataframe.

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
41
42
43
44
45
46
47
48
def df_row_names(df) -> List[Union[int, str]]:
    """Return a list of row names from a dataframe."""
    if df_type(df) == "vaex" or df_type(df) == "polars":
        return list(range(df_len(df)))
    elif df_type(df) == "pandas":
        return df.index.tolist()
    else:
        raise TypeError(f"{type(df)} not supported")

df_slice(df, start, stop) #

Return a subset of rows from a dataframe.

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
51
52
53
54
55
56
def df_slice(df, start: int, stop: int):
    """Return a subset of rows from a dataframe."""
    if df_type(df) == "pandas":
        return df.iloc[start:stop]
    else:
        return df[start:stop]

format_default(df, column, row_index, value) #

Format strings properly

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
90
91
92
93
94
def format_default(df, column, row_index, value):
    """Format strings properly"""
    if isinstance(value, float) and math.isnan(value):
        return "NaN"
    return str(value)

format_targets(df, column, row_index, value) #

Format the targets dataframe for special columns like sdss_id

Source code in src/sdss_explorer/dashboard/components/views/dataframe.py
235
236
237
238
239
240
241
242
243
244
245
246
def format_targets(df, column, row_index, value):
    """ Format the targets dataframe for special columns like sdss_id"""

    # check for zora base cookie, default to public site
    zbase = sl.lab.cookies.value.get('sdss_zora_base', 'dr19.sdss.org')
    base = f'http://{zbase}' if "localhost" in zbase else f'https://{zbase}/zora'

    # render a link to the zora target page for sdss_id
    if column == "sdss_id":
        url = f"{base}/target/{value}"
        return f'<a href="{url}" target="_blank">{value}</a>'
    return format_default(df, column, row_index, value)