1

I am trying to create a sunburst chart using a pandas dataframe and plotly express.

Here is dataframe, keep in mind this is only the first 10 rows, once it finishes with the suburbs for 2001, it jumps to the same suburbs for 2006, 2011, 2016

enter image description here

And here is my code

sunburst_chart = px.sunburst(
    sunburst_data,
    path=["year", "neighbourhood"],
    values="average_house_value",
    names="neighbourhood",  
)

sunburst_chart.show()

The error that is gives me is AttributeError: type object 'object' has no attribute 'dtype' and is referring to names="neighbourhood" but if I remove that line the error points to the line above, and repeat.

There are no duplicate rows, no nulls and the dtypes are

year                    int64
neighbourhood          object
average_house_value     int64
dtype: object

Any help is much appreciated, thank you

Derek O
  • 16,770
  • 4
  • 24
  • 43
Tom Weekes
  • 37
  • 3
  • 3
    Please don't post images. People can't copy data from the image and reproduce your problem - – Psidom Jun 27 '21 at 05:20
  • @Psidom What do you want me to put in its place? My df is like 700 rows – Tom Weekes Jun 27 '21 at 05:34
  • 1
    Paste a small data frame as text that can reproduce your problem. You don't need to put 700 rows in the post – Psidom Jun 27 '21 at 05:35
  • What version of numpy and pandas are you using? – Derek O Jun 27 '21 at 06:10
  • @TomWeekes I find it best to share samples of data [like this](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) – vestland Jun 27 '21 at 21:00

1 Answers1

2

Based on this issue and this bug report, I believe this is a numpy and pandas related bug that should be solved if you upgrade to the latest version of both libraries.

I created a sample DataFrame with the same columns and data types as yours, and after I downgraded to pandas 0.25.3 / numpy 1.20.0, I was able to reproduce your error:

AttributeError: type object 'object' has no attribute 'dtype'.

When I upgraded both libraries to numpy 1.21.0 / pandas 1.1.0, I was able to generate a sunburst plot:

import pandas as pd
import plotly.express as px

sunburst_data = pd.DataFrame({
    'year':[2001]*4 + [2002]*4 + [2003]*4,
    'neighbourhood': list('abcdefghijkl'),
    'average_house_value': list(range(100000,220000,10000))
})

sunburst_chart = px.sunburst(
    sunburst_data,
    path=["year", "neighbourhood"],
    values="average_house_value",
    names="neighbourhood",  
)

sunburst_chart.show()

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • 1
    @TomWeekes If this suggestion solved your problem, please consider marking it as the accepted answer and / or give it an upvote! – vestland Jun 27 '21 at 21:02