dexplo / bar_chart_race

Create animated bar chart races in Python with matplotlib
MIT License
1.35k stars 351 forks source link

'<=' not supported between instances of 'numpy.ndarray' and 'str' #65

Closed jeroenvermunt closed 2 years ago

jeroenvermunt commented 2 years ago

I have created the following dateframe for a bar race:

image

defined as:

bcr_df = pd.DataFrame({'joep': [0, 1],
                                       'jeroen': [0, 0],
                                       'timestamp': [datetime.date(2022, 8, 21), datetime.date(2022, 8, 22)]
                                }).set_index('timestamp', drop=True)

However, when running the standard bar chart race code it gives the following error:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_82080/833991757.py in <module>
----> 1 bcr.bar_chart_race(
      2     df=df,
      3     filename='test.mp4',
      4     orientation='h',
      5     sort='desc',

~/anaconda3/envs/bag/lib/python3.8/site-packages/bar_chart_race/_make_chart.py in bar_chart_race(df, filename, orientation, sort, n_bars, fixed_order, fixed_max, steps_per_period, period_length, interpolate_period, label_bars, bar_size, period_label, period_fmt, period_summary_func, perpendicular_bar_func, figsize, cmap, title, title_size, bar_label_size, tick_label_size, shared_fontdict, scale, writer, fig, dpi, bar_kwargs, filter_column_colors)
    776     These sizes are relative to plt.rcParams['font.size'].
    777     '''
--> 778     bcr = _BarChartRace(df, filename, orientation, sort, n_bars, fixed_order, fixed_max,
    779                         steps_per_period, period_length, interpolate_period, label_bars, bar_size,
    780                         period_label, period_fmt, period_summary_func, perpendicular_bar_func,

~/anaconda3/envs/bag/lib/python3.8/site-packages/bar_chart_race/_make_chart.py in __init__(self, df, filename, orientation, sort, n_bars, fixed_order, fixed_max, steps_per_period, period_length, interpolate_period, label_bars, bar_size, period_label, period_fmt, period_summary_func, perpendicular_bar_func, figsize, cmap, title, title_size, bar_label_size, tick_label_size, shared_fontdict, scale, writer, fig, dpi, bar_kwargs, filter_column_colors)
     45         self.html = self.filename is None
     46         self.df_values, self.df_ranks = self.prepare_data(df)
---> 47         self.fig, self.ax = self.get_fig(fig, dpi)
     48         self.col_filt = self.get_col_filt()
     49         self.bar_colors = self.get_bar_colors(cmap)

~/anaconda3/envs/bag/lib/python3.8/site-packages/bar_chart_race/_make_chart.py in get_fig(self, fig, dpi)
    237             self.figsize = fig.get_size_inches()
    238         else:
--> 239             fig, ax = self.create_figure(dpi)
    240         return fig, ax
    241 

~/anaconda3/envs/bag/lib/python3.8/site-packages/bar_chart_race/_make_chart.py in create_figure(self, dpi)
    243         fig = plt.Figure(figsize=self.figsize, dpi=dpi)
    244         limit = (.2, self.n_bars + .8)
--> 245         rect = self.calculate_new_figsize(fig)
    246         ax = fig.add_axes(rect)
    247         min_val = 1 if self.scale == 'log' else 0

~/anaconda3/envs/bag/lib/python3.8/site-packages/bar_chart_race/_make_chart.py in calculate_new_figsize(self, real_fig)
    276         ax = fig.add_subplot()
    277         fake_cols = [chr(i + 70) for i in range(self.df_values.shape[1])]
--> 278         max_val = self.df_values.max().max()
    279         # min_val = 1 if self.scale == 'log' else 0
    280         if self.orientation == 'h':

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/generic.py in max(self, axis, skipna, level, numeric_only, **kwargs)
  11220             **kwargs,
  11221         ):
> 11222             return NDFrame.max(self, axis, skipna, level, numeric_only, **kwargs)
  11223 
  11224         setattr(cls, "max", max)

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/generic.py in max(self, axis, skipna, level, numeric_only, **kwargs)
  10677         **kwargs,
  10678     ):
> 10679         return self._stat_function(
  10680             "max",
  10681             nanops.nanmax,

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/generic.py in _stat_function(self, name, func, axis, skipna, level, numeric_only, **kwargs)
  10647                 name, axis=axis, level=level, skipna=skipna, numeric_only=numeric_only
  10648             )
> 10649         return self._reduce(
  10650             func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  10651         )

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/series.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   4469                 )
   4470             with np.errstate(all="ignore"):
-> 4471                 return op(delegate, skipna=skipna, **kwds)
   4472 
   4473     def _reindex_indexer(

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/nanops.py in f(values, axis, skipna, **kwds)
    153                     result = alt(values, axis=axis, skipna=skipna, **kwds)
    154             else:
--> 155                 result = alt(values, axis=axis, skipna=skipna, **kwds)
    156 
    157             return result

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/nanops.py in new_func(values, axis, skipna, mask, **kwargs)
    408             mask = isna(values)
    409 
--> 410         result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
    411 
    412         if datetimelike:

~/anaconda3/envs/bag/lib/python3.8/site-packages/pandas/core/nanops.py in reduction(values, axis, skipna, mask)
   1041                 result = np.nan
   1042         else:
-> 1043             result = getattr(values, meth)(axis)
   1044 
   1045         result = _maybe_null_out(result, axis, mask, values.shape)

~/anaconda3/envs/bag/lib/python3.8/site-packages/numpy/core/_methods.py in _amax(a, axis, out, keepdims, initial, where)
     38 def _amax(a, axis=None, out=None, keepdims=False,
     39           initial=_NoValue, where=True):
---> 40     return umr_maximum(a, axis, None, out, keepdims, initial, where)
     41 
     42 def _amin(a, axis=None, out=None, keepdims=False,

TypeError: '<=' not supported between instances of 'numpy.ndarray' and 'str'

I can not make sense out of the trace back. does anyone know what is going wrong?

jeroenvermunt commented 2 years ago

I used the wrong dataframe. This issue can be deleted.

Excuse me for posting a non-issue.