dexplo / bar_chart_race

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

Baseball Example - ValueError: 'date' is not in list [ _make_chart.py in load_dataset(name)] #12

Closed leifulstrup closed 4 years ago

leifulstrup commented 4 years ago

df_baseball = bcr.load_dataset('baseball')

ValueError Traceback (most recent call last)

in 7 return {'s': s, 'x': .95, 'y': .07, 'ha': 'right', 'size': 8} 8 ----> 9 df_baseball = bcr.load_dataset('baseball') 10 df_baseball.head() ~/opt/anaconda3/lib/python3.7/site-packages/bar_chart_race/_make_chart.py in load_dataset(name) 371 ''' 372 return pd.read_csv(f'https://raw.githubusercontent.com/dexplo/bar_chart_race/master/data/{name}.csv', --> 373 index_col='date', parse_dates=['date']) ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision) 674 ) 675 --> 676 return _read(filepath_or_buffer, kwds) 677 678 parser_f.__name__ = name ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds) 446 447 # Create the parser. --> 448 parser = TextFileReader(fp_or_buf, **kwds) 449 450 if chunksize or iterator: ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds) 878 self.options["has_index_names"] = kwds["has_index_names"] 879 --> 880 self._make_engine(self.engine) 881 882 def close(self): ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine) 1112 def _make_engine(self, engine="c"): 1113 if engine == "c": -> 1114 self._engine = CParserWrapper(self.f, **self.options) 1115 else: 1116 if engine == "python": ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds) 1947 _validate_usecols_names(usecols, self.names) 1948 -> 1949 self._set_noconvert_columns() 1950 1951 self.orig_names = self.names ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _set_noconvert_columns(self) 2013 _set(k) 2014 else: -> 2015 _set(val) 2016 2017 elif isinstance(self.parse_dates, dict): ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _set(x) 2003 2004 if not is_integer(x): -> 2005 x = names.index(x) 2006 2007 self._reader.set_noconvert(x) ValueError: 'date' is not in list
tdpetrou commented 4 years ago

Same issue as before. I think you need to upgrade. pip install -U bar_chart_race

leifulstrup commented 4 years ago

Upgrade to V0.1.0 solved the read problem but got this error now:

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

bcr.bar_chart_race(df_baseball, period_length=1000, fixed_max=True, fixed_order=True, n_bars=10, figsize=(5, 3), period_fmt='Season {x:,.0f}', title='Top 10 Home Run Hitters by Season Played')



TypeError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _try_kind_sort(arr) 2947 # if kind==mergesort, it can fail for object dtype -> 2948 return arr.argsort(kind=kind) 2949 except TypeError:

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

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)

in 2 fixed_max=True, fixed_order=True, n_bars=10, 3 figsize=(5, 3), period_fmt='Season {x:,.0f}', ----> 4 title='Top 10 Home Run Hitters by Season Played') ~/opt/anaconda3/lib/python3.7/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) 780 period_label, period_fmt, period_summary_func, perpendicular_bar_func, 781 figsize, cmap, title, title_size, bar_label_size, tick_label_size, --> 782 shared_fontdict, scale, writer, fig, dpi, bar_kwargs, filter_column_colors) 783 return bcr.make_animation() 784 ~/opt/anaconda3/lib/python3.7/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) 44 self.bar_kwargs = self.get_bar_kwargs(bar_kwargs) 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() ~/opt/anaconda3/lib/python3.7/site-packages/bar_chart_race/_make_chart.py in prepare_data(self, df) 139 def prepare_data(self, df): 140 if self.fixed_order is True: --> 141 last_values = df.iloc[-1].sort_values(ascending=False) 142 cols = last_values.iloc[:self.n_bars].index 143 df = df[cols] ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in sort_values(self, axis, ascending, inplace, kind, na_position, ignore_index) 2960 idx = ibase.default_index(len(self)) 2961 -> 2962 argsorted = _try_kind_sort(arr[good]) 2963 2964 if is_list_like(ascending): ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _try_kind_sort(arr) 2950 # stable sort not available for object dtype 2951 # uses the argsort default quicksort -> 2952 return arr.argsort(kind="quicksort") 2953 2954 arr = self._values TypeError: '<' not supported between instances of 'numpy.ndarray' and 'str'
tdpetrou commented 4 years ago

@leifulstrup The baseball dataset will not work directly. It must be "prepared" with `prepared_long_data" first.