ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead
Asked Answered
G

7

24

I am trying to plot a histogram using seaborn. When I try to set kde=True this error is returned: ValueError: Multi-dimensional indexing (e.g. obj[:, None]) is no longer supported. Convert to a numpy array before indexing instead.

sns.histplot(data=df, x='age', kde=True);

How can I solve this?

Given answered 5/4, 2023 at 11:55 Comment(0)
S
12

This can also happen in Matplotlib. I have a virtual environment with matplotlib=3.3.0 and pandas=2.0.2.

A workaround is to use the dataframe's values attribute which will return a numpy array, which can then be used in the plotting function:

plt.plot(df['var_name'].values, df['other_var_name'].values)
Sharpe answered 22/6, 2023 at 15:23 Comment(2)
The documentation states: Warning: We recommend using DataFrame.to_numpy() instead.Cardinale
Upvoted. I added an answer too, with more detail and the recommendation to use .to_numpy() instead.Cardinale
P
11

I believe that you have an incompatibility between your version of matplotlib and your version of pandas, with seaborn caught in the middle (source: https://github.com/mwaskom/seaborn/issues/3312)

Psychoneurotic answered 5/4, 2023 at 12:9 Comment(0)
C
2

Apparently you must convert the object to a NumPy array via pandas.DataFrame.to_numpy() for plotting with matplotlib.pyplot.plot().

For this error:

ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.

Full context:

Traceback (most recent call last):
  File "/home/gabriel/my/file.py", line 120, in <module>
    main()
  File "/home/gabriel/my/file.py", line 98, in main
    plt.plot(
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2757, in plot
    return gca().plot(
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 1632, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 312, in __call__
    yield from self._plot_args(this, kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 487, in _plot_args
    x = _check_1d(xy[0])
  File "/usr/lib/python3/dist-packages/matplotlib/cbook/__init__.py", line 1327, in _check_1d
    ndim = x[:, None].ndim
  File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1072, in __getitem__
    return self._get_with(key)
  File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1082, in _get_with
    return self._get_values_tuple(key)
  File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/series.py", line 1122, in _get_values_tuple
    disallow_ndim_indexing(result)
  File "/home/gabriel/.local/lib/python3.10/site-packages/pandas/core/indexers/utils.py", line 341, in disallow_ndim_indexing
    raise ValueError(
ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.

.values works:

plt.plot(df['var_name'].values, df['other_var_name'].values)

...but the pandas.DataFrame.values documentation states:

Warning: We recommend using DataFrame.to_numpy() instead.

So, .to_numpy() is recommended instead:

plt.plot(df['var_name'].to_numpy(), df['other_var_name'].to_numpy())
Cardinale answered 13/9, 2023 at 3:54 Comment(0)
M
0

You may upgrade your packages, but it may not be compatible with other libraries such as TensorFlow, so, you may have to downgrade the package.

Malenamalet answered 30/5, 2023 at 10:27 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Piragua
A
0

This code below:

plt.plot(df['var_name'].to_numpy(), df['other_var_name'].to_numpy())

IT WORKED PERFECTLY for ME as WELL! Thanks!

Airdrop answered 15/11, 2023 at 22:59 Comment(0)
T
0

workaround:

step 1. check np.ndim(your_df) for your_df. If output print(np.ndim(your_df)): 1, then go to step 2

step 2. find and change utils.py via nano, you need set "if np.ndim(result) > 2":

def disallow_ndim_indexing(result) -> None: """ Helper function to disallow multi-dimensional indexing on 1D Series/Index.

GH#27125 indexer like idx[:, None] expands dim, but we cannot do that
and keep an index, so we used to return ndarray, which was deprecated
in GH#30588.
    nnv_comment: default np.ndim(result) > 1
"""     
if np.ndim(result) > 2:
    raise ValueError(
        "Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer "
        "supported. Convert to a numpy array before indexing instead."
    )

step 3. reboot and try again

Tawnatawney answered 3/2 at 16:35 Comment(0)
M
0

Upgrading matplotlib worked for me.

Marsiella answered 15/4 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.