Forum Discussion

MarcialMartín's avatar
MarcialMartín
New Member
2 years ago

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

  • 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.

    • MarcialMartín's avatar
      MarcialMartín
      New Member

      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:

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

       

      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.