Skip to content

Pandas - AttributeError: 'DataFrame' object has no attribute 'map' visualize.py #7

@sourdough-bread

Description

@sourdough-bread

This error is due to the pandas not being up-to-date.

If you want to maintain your pandas version, here is the visualisation function that works for pandas version < 2.1

def visualize(sol, factory, highlight_cover=False):
    weeks = [f"Week {i + 1}" for i in range(factory.data.horizon // 7)]
    weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    nurses = factory.data.staff['name'].tolist()

    df = pd.DataFrame(sol,
                      columns=pd.MultiIndex.from_product((weeks, weekdays)),
                      index=factory.data.staff.name)

    mapping = factory.idx_to_name
    df = df.applymap(lambda v: mapping[v] if v is not None and v < len(mapping) else '')  # convert to shift names

    for shift_type in factory.shift_name_to_idx:
        if shift_type == "F":
            continue
        sums = (df == shift_type).sum()  # cover for each shift type
        req = factory.data.cover["Requirement"][factory.data.cover["ShiftID"] == shift_type]
        req.index = sums.index
        df.loc[f'Cover {shift_type}'] = sums.astype(str) + "/" + req.astype(str)
    df["Total shifts"] = (df != "F").sum(axis=1)  # shifts done by nurse

    subset = (df.index.tolist()[:-len(factory.data.shifts)], df.columns[:-1])
    styler = df.style\
        .applymap(lambda v: 'border: 1px solid black', subset=subset)\
        .applymap(color_shift, factory=factory, subset=subset) \
        .set_table_styles([{'selector': '.data', 'props': [('text-align', 'center')]},
                                       {'selector': '.col_heading', 'props': [('text-align', 'center')]},
                                       {'selector': '.col7', 'props': [('border-left',"2px solid black")]}
                                       ])

    if highlight_cover is True:

        def highlight(val):
            fill, req = val.split('/')
            if fill == req:
                return ''
            return 'color : red'

        subset = (df.index.tolist()[-len(factory.data.shifts):], df.columns[:-1])
        styler = styler.applymap(highlight, subset=subset)
    styler
    
    return styler

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions