You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to build a bar chart with on the x-axis a binned result_perc.
Because I have too many records I have to bin them first using pandas.
I do this by using an IntervalIndex:
width = 10
bins = pd.IntervalIndex.from_breaks([round(x, 1) for x in list(np.linspace(-width, width, (width*10+1), endpoint=True))])
# aggregate per bin
aggregations = {"amount_eur": ['sum']}
df = df[['result_perc_bin', 'amount_eur']].groupby(by=['result_perc_bin']).agg(aggregations)
df = df.reset_index()
df.columns = df.columns.get_level_values(0)
I now have a df with reduced records which I can use to create a bar chart.
But Altair gives the error:
TypeError: Object of type 'Interval' is not JSON serializable
To fix this I convert the dtype of result_perc_bin (the IntervalIndex) which looks like this
There's some ongoing work in the altair-transform repo to allow pushing these kinds of data transformations directly from the Altair grammar into Pandas, but it's not quite fully-featured yet.
Edit: note that this second approach is similar to how you must express the spec if you use an explicit bin transform; for example:
Hello,
I'm trying to build a bar chart with on the x-axis a binned result_perc.
Because I have too many records I have to bin them first using pandas.
I do this by using an IntervalIndex:
I now have a df with reduced records which I can use to create a bar chart.
But Altair gives the error:
To fix this I convert the dtype of result_perc_bin (the IntervalIndex) which looks like this
df.result_perc_bin = df.result_perc_bin.astype('str').astype('category')
But this conversion screws up the order of the labels, so I need a sort_order:
result_perc_bin_sort_order = list(bins.astype(str))
The code to create the chart:
Question 1:
Is there a better way to work with bins? A way where I don't have to back up the sort_order?
The above code actually solves my problem but it breaks when I try to merge 2 charts.
The output is:
As you can see the chart lost the result_perc_bin_sort_order again.
Question 2:
How can I sort the x-axis correctly in this case?
,Thx for your ideas
The text was updated successfully, but these errors were encountered: