7

I have a ValueError: 'object too deep for desired array' in a Python program. I have this error while using numpy.digitize.
I think it's how I use Pandas DataFrames:
To keep it simple (because this is done through an external library), I have a list in my program but the library needs a DataFrame so I do something like this:

ts = range(1000)
df = pandas.DataFrame(ts)
res = numpy.digitize(df.values, bins)

But then it seems like df.values is an array of lists instead of an array of floats. I mean:

array([[   0],
   [   1],
   [   2],
   ..., 
   [997],
   [998],
   [999]], dtype=int64)

Help please, I spent too much time on this.

othymomo
  • 105
  • 1
  • 2
  • 7
  • 1
    I think if you upgrade to the latest version of `numpy(1.11)`, you could pass an array of any shape to `np.digitize` – Nickil Maveli Sep 13 '16 at 16:13
  • Does this answer your question? [What does "ValueError: object too deep for desired array" mean and how to fix it?](https://stackoverflow.com/questions/15923081/what-does-valueerror-object-too-deep-for-desired-array-mean-and-how-to-fix-it) – mkrieger1 Feb 02 '22 at 13:03

1 Answers1

4

Try this:

numpy.digitize(df.iloc[:, 0], bins)

You are trying to get the values from a whole DataFrame. That is why you get the 2D array. Each row in the array is a row of the DataFrame.

Kartik
  • 8,347
  • 39
  • 73
  • This worked but I actually don't understand it. Can I create the DataFrame so that I get a 1D array (maybe transpose the initial time series)? – othymomo Sep 13 '16 at 16:33
  • 1
    Create a Series instead then (`s = pandas.Series(ts)`). Series are always 1D. It depends on your use case. If you get a DataFrame, you need to index it such that you pass only a column to `digitize`. If your data is 1D, you can use Series from the outset. – Kartik Sep 13 '16 at 16:46