has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.92k stars 210 forks source link

facet_grid throws an error: ValueError: No objects to concatenate #734

Closed vishv-jeet closed 6 months ago

vishv-jeet commented 7 months ago

Minimum example to produce the bug using plotnine version 0.12.3

import numpy as np import polars as pl from plotnine import ggplot, geom_point, facet_grid, aes tickers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] keys = ['foo', 'bar', 'baz'] periods = [i for i in range(660)] df = pl.DataFrame([(i, j, k) for i in tickers for j in keys for k in periods], ['ticker', 'key', 'period'])\ .with_columns(value=pl.Series(np.random.rand(19800)))

( ggplot(df, aes(x='period', y='value'))

""" ########################################################

File .venv/lib/python3.8/site-packages/IPython/core/formatters.py:708, in PlainTextFormatter.call(self, obj) 701 stream = StringIO() 702 printer = pretty.RepresentationPrinter(stream, self.verbose, 703 self.max_width, self.newline, 704 max_seq_length=self.max_seq_length, 705 singleton_pprinters=self.singleton_printers, 706 type_pprinters=self.type_printers, 707 deferred_pprinters=self.deferred_printers) --> 708 printer.pretty(obj) 709 printer.flush() 710 return stream.getvalue()

File .venv/lib/python3.8/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj) 407 return meth(obj, self, cycle) 408 if cls is not object \ 409 and callable(cls.dict.get('repr')): --> 410 return _repr_pprint(obj, self, cycle) 412 return _default_pprint(obj, self, cycle) 413 finally:

File .venv/lib/python3.8/site-packages/IPython/lib/pretty.py:778, in _reprpprint(obj, p, cycle) 776 """A pprint that just redirects to the normal repr function.""" 777 # Find newlines and replace them with p.break() --> 778 output = repr(obj) 779 lines = output.splitlines() 780 with p.group():

File .venv/lib/python3.8/site-packages/plotnine/ggplot.py:114, in ggplot.repr(self) 110 def repr(self) -> str: 111 """ 112 Print/show the plot 113 """ --> 114 figure = self.draw(show=True) 116 dpi = figure.get_dpi() 117 W = int(figure.get_figwidth() * dpi)

File .venv/lib/python3.8/site-packages/plotnine/ggplot.py:224, in ggplot.draw(self, show) 222 self = deepcopy(self) 223 with plot_context(self, show=show): --> 224 self._build() 226 # setup 227 figure, axs = self._create_figure()

File .venv/lib/python3.8/site-packages/plotnine/ggplot.py:328, in ggplot._build(self) 325 layout.map_position(layers) 327 # Apply and map statistics --> 328 layers.compute_statistic(layout) 329 layers.map_statistic(self) 331 # Prepare data in geoms 332 # e.g. from y and width to ymin and ymax

File /.venv/lib/python3.8/site-packages/plotnine/layer.py:471, in Layers.compute_statistic(self, layout) 469 def compute_statistic(self, layout: Layout): 470 for l in self: --> 471 l.compute_statistic(layout)

File .venv/lib/python3.8/site-packages/plotnine/layer.py:284, in layer.compute_statistic(self, layout) 282 data = self.stat.use_defaults(data) 283 data = self.stat.setup_data(data) --> 284 data = self.stat.compute_layer(data, params, layout) 285 self.data = data

File .venv/lib/python3.8/site-packages/plotnine/stats/stat.py:306, in stat.compute_layer(cls, data, params, layout) 303 pscales = layout.get_scales(pdata["PANEL"].iat[0]) 304 return cls.compute_panel(pdata, pscales, **params) --> 306 return groupby_apply(data, "PANEL", fn)

File .venv/lib/python3.8/site-packages/plotnine/utils.py:597, in groupbyapply(df, cols, func, *args, **kwargs) 593 for , d in df.groupby(cols, observed=True): 594 # function fn should be free to modify dataframe d, therefore 595 # do not mark d as a slice of df i.e no SettingWithCopyWarning 596 lst.append(func(d, *args, **kwargs)) --> 597 return pd.concat(lst, axis=axis, ignore_index=True)

File .venv/lib/python3.8/site-packages/pandas/core/reshape/concat.py:372, in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy) 369 elif copy and using_copy_on_write(): 370 copy = False --> 372 op = _Concatenator( 373 objs, 374 axis=axis, 375 ignore_index=ignore_index, 376 join=join, 377 keys=keys, 378 levels=levels, 379 names=names, 380 verify_integrity=verify_integrity, 381 copy=copy, 382 sort=sort, 383 ) 385 return op.get_result()

File .venv/lib/python3.8/site-packages/pandas/core/reshape/concat.py:429, in _Concatenator.init(self, objs, axis, join, keys, levels, names, ignore_index, verify_integrity, copy, sort) 426 objs = list(objs) 428 if len(objs) == 0: --> 429 raise ValueError("No objects to concatenate") 431 if keys is None: 432 objs = list(com.not_none(*objs))

ValueError: No objects to concatenate

"""

has2k1 commented 7 months ago

This is a bug. The problem is the column with the name "key". For now you can change it to anything else.

vishv-jeet commented 7 months ago

Many thanks for the acknowledgement and the solution.VishySent from my iPhoneOn Dec 8, 2023, at 3:46 AM, Hassan Kibirige @.***> wrote: This is a bug. The problem is the column with the name "key". For now you can change it to anything else.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>