-
Notifications
You must be signed in to change notification settings - Fork 54
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
'apply_style_by_indexes' fails if the dataframe has a column called "index" #108
Comments
Hi. Can you please provide a better example? If your dataframe has multi-index please be aware that I've tried the below contrived example and it seems to be working: from itertools import product
from styleframe import StyleFrame, Styler
TRAFFIC_LIGHT_COLORS = ('yellow', 'green', 'red')
TRAFFIC_LIGHT_COLS = ('yellow', 'green', 'red')
excel_writer = StyleFrame.ExcelWriter('output.xlsx')
sf = StyleFrame({'yellow': ['yellow', 2], 'green': [1, 'green'], 'red': [1, 'red']})
for color, col_name in product(TRAFFIC_LIGHT_COLORS, TRAFFIC_LIGHT_COLS):
styler = Styler(bg_color=color.lower())
sf.apply_style_by_indexes(indexes_to_style=sf[sf[col_name] == color],
cols_to_style=col_name,
styler_obj=styler,
overwrite_default_style=False)
sf.to_excel(excel_writer).save() |
Hi In the first message I added the signature of the method. Could you run your snippet of code by using this startup?
|
I still can't reproduce the issue. Full code: import pandas as pd
from itertools import product
from styleframe import StyleFrame, Styler
TRAFFIC_LIGHT_COLORS = ('yellow', 'green', 'red')
TRAFFIC_LIGHT_COLS = ('status1', 'status2', 'status3')
df = pd.DataFrame({"status1": ["yellow", "green", "yellow"],
"status2": ["yellow", "red", "red"],
"status3": ["green", "green", "green"]})
sf = StyleFrame(df)
excel_writer = StyleFrame.ExcelWriter('output.xlsx')
for color, col_name in product(TRAFFIC_LIGHT_COLORS, TRAFFIC_LIGHT_COLS):
styler = Styler(bg_color=color.lower())
sf.apply_style_by_indexes(indexes_to_style=sf[sf[col_name] == color],
cols_to_style=col_name,
styler_obj=styler,
overwrite_default_style=False)
sf.to_excel(excel_writer).save() from styleframe import version
print(version.get_all_versions())
|
I could reproduce it by exporting just 2 rows of my dataset and plotting some characteristics of it. import pandas as pd
from itertools import product
from styleframe import StyleFrame, Styler
df = pd.read_pickle("df_tst.pkl")
df.info()
TRAFFIC_LIGHT_COLORS = ('YELLOW', 'GREEN', 'RED')
TRAFFIC_LIGHT_COLS = ('STATUS1', 'STATUS2', 'STATUS3', 'STATUS4', 'STATUS5')
sf = StyleFrame(df)
excel_writer = StyleFrame.ExcelWriter('output.xlsx')
for color, col_name in product(TRAFFIC_LIGHT_COLORS, TRAFFIC_LIGHT_COLS):
styler = Styler(bg_color=color.lower())
sf.apply_style_by_indexes(indexes_to_style=sf[sf[col_name] == color],
cols_to_style=col_name,
styler_obj=styler,
overwrite_default_style=False)
from styleframe import version
print(version.get_all_versions())
Ps. how do you format so well your code and outputs? Are you pasting from a notebook? |
Can you please share the actual I'm formatting code snippets with code blocks and the
|
Thanks for your answer again. Namely: import pandas as pd
from itertools import product
from styleframe import StyleFrame, Styler
df_pkl = pd.read_pickle("df_tst.pkl")
df_pkl.info()
df_pkl.to_csv("df_test.csv", index=False)
df_csv = pd.read_csv("df_test.csv")
df = df_csv Then by running TRAFFIC_LIGHT_COLORS = ('YELLOW', 'GREEN', 'RED')
TRAFFIC_LIGHT_COLS = ('STATUS1', 'STATUS2', 'STATUS3', 'STATUS4', 'STATUS5')
sf = StyleFrame(df)
excel_writer = StyleFrame.ExcelWriter('output.xlsx')
for color, col_name in product(TRAFFIC_LIGHT_COLORS, TRAFFIC_LIGHT_COLS):
styler = Styler(bg_color=color.lower())
sf.apply_style_by_indexes(indexes_to_style=sf[sf[col_name] == color],
cols_to_style=col_name,
styler_obj=styler,
overwrite_default_style=False) I get the error mentioned in the above comments. |
Great, I've managed to reproduce the issue. The reason is that you have a column called df = pd.DataFrame({'index': [1, 2]})
sf = StyleFrame(df)
sf.apply_style_by_indexes(sf.data_df.index, Styler())
AttributeError: 'Series' object has no attribute 'get_loc'
As a quick workaround I'd suggest that you rename the column before converting to df = pd.DataFrame({'index': [1, 2]})
sf = StyleFrame(df.rename(columns={'index': '_index'}))
sf.apply_style_by_indexes(sf.data_df.index, Styler())
# NO ERROR I'll have to do some digging to permanently fix this. |
Oh great, that's a quick workaround. Thank you. Best, |
Fixed in version 4.2 |
Hi, I am using the following versions:
The following code:
where color can be
YELLOW
,GREEN
, etc. and col_name are some of the columns of my DataFrame transformed into a StyleFrame.Though I get the following error:
The text was updated successfully, but these errors were encountered: