secondmind-labs/trieste

Pandas error in documentation notebook

markvdw opened this issue · 6 comments

Describe the bug
When running the notebook expected_improvement.pct, cell 2 results in an error from pandas noting ValueError: Index data must be 1-dimensional.

The issue seems to be in making a dataframe with a 2D index.

To reproduce
Steps to reproduce the behaviour:

  1. Download Trieste
  2. Install Trieste
  3. Run expected_improvement.pct

Expected behaviour
The tutorial notebook runs out of the box.

System information

  • OS: MacOS
  • Python version: 3.10
  • Trieste version: Cloned from repo
  • TensorFlow version: 2.10.1
  • GPflow version: 2.7

The error Traceback is below.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 9
      6 search_space = ScaledBranin.search_space  # predefined search space
      7 search_space = Box([0, 0], [1, 1])  # define the search space directly
----> 9 fig = plot_function_plotly(
     10     scaled_branin,
     11     search_space.lower,
     12     search_space.upper,
     13     grid_density=20,
     14 )
     15 fig.update_layout(height=400, width=400)
     16 fig.show()

File ~/miniconda3/lib/python3.10/site-packages/trieste/experimental/plotting/plotting_plotly.py:252, in plot_function_plotly(obj_func, mins, maxs, grid_density, title, xlabel, ylabel, alpha)
    250 for k in range(n_output):
    251     f = F[:, k]
--> 252     fig = add_surface_plotly(xx, yy, f, fig, alpha=alpha, figrow=1, figcol=k + 1)
    253     fig.update_xaxes(title_text=xlabel, row=1, col=k + 1)
    254     fig.update_yaxes(title_text=ylabel, row=1, col=k + 1)

File ~/miniconda3/lib/python3.10/site-packages/trieste/experimental/plotting/plotting_plotly.py:91, in add_surface_plotly(xx, yy, f, fig, alpha, figrow, figcol)
     69 def add_surface_plotly(
     70     xx: TensorType,
     71     yy: TensorType,
   (...)
     76     figcol: int = 1,
     77 ) -> go.Figure:
     78     """
     79     Adds a surface to an existing plotly subfigure
     80 
   (...)
     88     :return: updated plotly figure
     89     """
---> 91     d = pd.DataFrame(f.reshape([xx.shape[0], yy.shape[1]]), index=xx, columns=yy)
     93     fig.add_trace(
     94         go.Surface(z=d, x=xx, y=yy, showscale=False, opacity=alpha, colorscale="viridis"),
     95         row=figrow,
     96         col=figcol,
     97     )
     98     return fig

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/frame.py:721, in DataFrame.__init__(self, data, index, columns, dtype, copy)
    711         mgr = dict_to_mgr(
    712             # error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
    713             # attribute "name"
   (...)
    718             typ=manager,
    719         )
    720     else:
--> 721         mgr = ndarray_to_mgr(
    722             data,
    723             index,
    724             columns,
    725             dtype=dtype,
    726             copy=copy,
    727             typ=manager,
    728         )
    730 # For data is list-like, or Iterable (will consume into list)
    731 elif is_list_like(data):

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/internals/construction.py:345, in ndarray_to_mgr(values, index, columns, dtype, copy, typ)
    335     values = sanitize_array(
    336         values,
    337         None,
   (...)
    341         allow_2d=True,
    342     )
    344 # _prep_ndarraylike ensures that values.ndim == 2 at this point
--> 345 index, columns = _get_axes(
    346     values.shape[0], values.shape[1], index=index, columns=columns
    347 )
    349 _check_values_indices_shape_match(values, index, columns)
    351 if typ == "array":

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/internals/construction.py:748, in _get_axes(N, K, index, columns)
    746     index = default_index(N)
    747 else:
--> 748     index = ensure_index(index)
    750 if columns is None:
    751     columns = default_index(K)

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/indexes/base.py:7376, in ensure_index(index_like, copy)
   7374         return Index._with_infer(index_like, copy=copy, tupleize_cols=False)
   7375 else:
-> 7376     return Index._with_infer(index_like, copy=copy)

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/indexes/base.py:717, in Index._with_infer(cls, *args, **kwargs)
    715 with warnings.catch_warnings():
    716     warnings.filterwarnings("ignore", ".*the Index constructor", FutureWarning)
--> 717     result = cls(*args, **kwargs)
    719 if result.dtype == _dtype_obj and not result._is_multi:
    720     # error: Argument 1 to "maybe_convert_objects" has incompatible type
    721     # "Union[ExtensionArray, ndarray[Any, Any]]"; expected
    722     # "ndarray[Any, Any]"
    723     values = lib.maybe_convert_objects(result._values)  # type: ignore[arg-type]

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/indexes/base.py:541, in Index.__new__(cls, data, dtype, copy, name, tupleize_cols, **kwargs)
    538             return cls(arr, dtype, copy=copy, name=name, **kwargs)
    540 klass = cls._dtype_to_subclass(arr.dtype)
--> 541 arr = klass._ensure_array(arr, dtype, copy)
    542 disallow_kwargs(kwargs)
    543 return klass._simple_new(arr, name)

File ~/miniconda3/lib/python3.10/site-packages/pandas/core/indexes/numeric.py:176, in NumericIndex._ensure_array(cls, data, dtype, copy)
    172     subarr = data
    174 if subarr.ndim > 1:
    175     # GH#13601, GH#20285, GH#27125
--> 176     raise ValueError("Index data must be 1-dimensional")
    178 subarr = np.asarray(subarr)
    179 return subarr

ValueError: Index data must be 1-dimensional

I have the same error. However, it worked with pandas<1.0

I have the same issue. Any idea how to fix it without downgrading pandas?

I'm afraid the notebooks currently don't support pandas 1.0 (or Python 3.10 for that matter). We hope this will be fixed, but it looks as it will be surprisingly fiddly it is not currently viewed as a high priority as it doesn't impact the package itself. Sorry!

@uri-granta how come notebooks cannot run with 3.10?

There are currently three issues to resolve:

  1. we currently pin sphinx to 3.5.4 to work around plotly/plotly.js#4563, but that version breaks with Python 3.10
  2. we currently pin pandas to <1.0.0 (and rely on it in a fair few places), which similarly breaks with Python 3.10
  3. we depend on box2d, which doesn't provide wheels for Python 3.10 or a source package: see https://stackoverflow.com/questions/70323852/error-trying-to-install-python-package-box2d

This has now been fixed in #728, and the other issues in #734 and #738 meaning that the notebooks now also work with Python 3.10. We have not tested using MacOS but expect that the notebook should now run fine?