Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
MarcialMartín
New Member

TypeError: unhashable type: 'slice'

Hi community.

 

I have a visual based on python that works fine on my desktop version but when I publish it I received the following error: TypeError: unhashable type: 'slice' 

 

The script code is:

 

 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# Paste or type your script code here:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

dataset["Timestamp"] = dataset["Year"].loc[:].astype(str) +" "+ dataset["Month"].loc[:].astype(str)


principles = dataset.drop(columns=["Timestamp", "Month", "Year"]).loc[0].index.tolist()
timestamp = dataset["Timestamp"].loc[:]
heatmap = np.array([dataset["Principle_1"].loc[:].round(2),
                    dataset["Principle_2"].loc[:].round(2),
                    dataset["Principle_3"].loc[:].round(2),
                    dataset["Principle_4"].loc[:].round(2),
                    dataset["Principle_5"].loc[:].round(2),
                    dataset["Principle_6"].loc[:].round(2),
                    dataset["Principle_7"].loc[:].round(2),
                    dataset["Principle_8"].loc[:].round(2)])

size = 10
color = "#4F4F4F"

fig, ax = plt.subplots(figsize=(size*1.5, size))
im = ax.imshow(heatmap, cmap="RdYlGn", vmin=-2.00, vmax=2.00)

# Create a colorbar
cbar = ax.figure.colorbar(im, ax=ax, fraction=0.039, pad=0.03)
cbar.ax.tick_params(color="#FFFFFF")
cbar.outline.set_edgecolor("#FFFFFF")
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color=color, size=size*1.2)

# Show all ticks and label them with the respective list entries
ax.set_xticks(np.arange(len(timestamp)))
ax.set_xticklabels(timestamp)
ax.set_yticks(np.arange(len(principles)))
ax.set_yticklabels(principles)

#ax.set_xticks(np.arange(len(timestamp)), labels=timestamp, size=size*1.2, color=color)
#ax.set_yticks(np.arange(len(principles)), labels=principles, size=size*1.2, color=color)

# Rotate the tick labels and set their alignment, so they don't collide
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(len(principles)):
	for j in range(len(timestamp)):
		text = ax.text(j, i, format(heatmap[i, j], ".2f"), ha="center", va="center", color="#383838", size=12)

# Remove the spines and create grid for ax
ax.spines[:].set_visible(False)
ax.set_xticks(np.arange(len(timestamp)+1)-.5, minor=True)
ax.set_yticks(np.arange(len(principles)+1)-.5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=4)
ax.tick_params(which="minor", bottom=False, left=False)

fig.tight_layout()

plt.show()

 

 

 

 

Any idea? I have tried .loc and iloc with same result.

 

Thanks in advance

 

 

 

 

 

2 REPLIES 2
Sahir_Maharaj
Super User
Super User

Hello @MarcialMartín,

 

Can you please try this updated script:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Assuming 'dataset' is a predefined DataFrame
dataset["Timestamp"] = dataset["Year"].astype(str) + " " + dataset["Month"].astype(str)

principles = dataset.drop(columns=["Timestamp", "Month", "Year"]).iloc[0].index.tolist()
timestamp = dataset["Timestamp"]
heatmap = np.array([
    dataset["Principle_1"].round(2),
    dataset["Principle_2"].round(2),
    dataset["Principle_3"].round(2),
    dataset["Principle_4"].round(2),
    dataset["Principle_5"].round(2),
    dataset["Principle_6"].round(2),
    dataset["Principle_7"].round(2),
    dataset["Principle_8"].round(2)
])

# ... rest of your plotting code remains the same ...

Should you require any further assistance, please do not hesitate to reach out to me.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Hi @Sahir_Maharaj.

 

Thank you so much for your answer. I have tried your solution but the problem persists.

 

In the desktop version it is working well:

MarcialMartn_0-1703775576097.png

But when I publish it and try to visualice in the web version of PowerBI I received this:

MarcialMartn_1-1703775637726.png

 

MarcialMartn_2-1703775666440.png

Any support is welcome cause the only thing that I can imagine is that the desktop version is not supporting the libraries version.

 

Thanks in advance.

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.