Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wish: MultiIndex support #129

Open
buhtz opened this issue Jun 28, 2022 · 4 comments
Open

Wish: MultiIndex support #129

buhtz opened this issue Jun 28, 2022 · 4 comments

Comments

@buhtz
Copy link
Contributor

buhtz commented Jun 28, 2022

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

@stale
Copy link

stale bot commented Sep 20, 2022

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

@stale stale bot added the stale label Sep 20, 2022
@buhtz
Copy link
Contributor Author

buhtz commented Sep 20, 2022

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

@DeepSpace2
Copy link
Owner

Hi, I'm looking into implementing this.

@DeepSpace2 DeepSpace2 added this to the 4.2 milestone Sep 30, 2022
@DeepSpace2
Copy link
Owner

DeepSpace2 commented Oct 4, 2022

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 DeepSpace2 self-assigned this Oct 4, 2022
@DeepSpace2 DeepSpace2 removed this from the 4.2 milestone Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants