StyleFrame icon indicating copy to clipboard operation
StyleFrame copied to clipboard

Wish: MultiIndex support

Open buhtz opened this issue 3 years ago • 4 comments

Because of #62 I am aware that StyleFrame currently not support pandas.MultiIndex . Would be great to have this in the future because multi indexes are a part of formatting a table.

Here is a small example illustrating the current behaviour:

#!/usr/bin/env python3
import os
import sys
import pathlib
import pandas
import styleframe
import styleframe.utils

print(styleframe._versions_)

df = pandas.DataFrame(
    data={
        'idx1': list('AABB'),
        'idx2': list('xyxy'),
        'X': [1234, 345, 33123, 2],
        'Y': [2.4133, 42.3, 3, 13.344]
        }
    )
df = df.set_index(['idx1', 'idx2'])
print(df)

file_path = pathlib.Path.cwd() / 'test.xlsx'

default_style = styleframe.Styler(
    font_size=14, font=styleframe.utils.fonts.aharoni)
sf = styleframe.StyleFrame(df, styler_obj=default_style)

sf.to_excel(file_path, index=True).save()
os.system(str(file_path))

The console output:

Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
pandas 1.3.0
openpyxl 3.0.9
StyleFrame 4.1
               X        Y
idx1 idx2
A    x      1234   2.4133
     y       345  42.3000
B    x     33123   3.0000
     y         2  13.3440

The Excel result: image

buhtz avatar Jun 28 '22 08:06 buhtz

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

stale[bot] avatar Sep 20 '22 19:09 stale[bot]

The project seems dead. If not please deactivate the bot. If so please update your README.md to indicate the current project status.

buhtz avatar Sep 20 '22 19:09 buhtz

Hi, I'm looking into implementing this.

DeepSpace2 avatar Sep 23 '22 14:09 DeepSpace2

The work on this is almost done on the multi-index branch, however there is one annoying caveat that keeps me from merging and releasing this.

Apparently, pandas.MultiIndex object re-use objects it thinks are the same. To make matters worse, index objects are immutable (ie unassignable, df.index[i] = ... causes a TypeError).

So, this code

import pandas
from styleframe import StyleFrame, Styler

df = pandas.DataFrame(
    {
        'idx1': list('AABB'),
        'idx2': list('zywz'),
        'idx3': list('1212'),
        'X': [1234, 345, 33123, 2],
        'Y': [2.4133, 42.3, 3, 13.344]
    }
)

df = df.set_index(['idx1', 'idx2', 'idx3'])

sf = StyleFrame(df)

sf.apply_style_by_indexes(sf[sf['Y'] == 3], Styler(bg_color='yellow'))

sf.to_excel('test.xlsx', index=True).save()

creates this

image

instead of this

image

DeepSpace2 avatar Oct 04 '22 19:10 DeepSpace2