81

I am receiving the error: ValueError: Wrong number of items passed 3, placement implies 1, and I am struggling to figure out where, and how I may begin addressing the problem.

I don't really understand the meaning of the error; which is making it difficult for me to troubleshoot. I have also included the block of code that is triggering the error in my Jupyter Notebook.

The data is tough to attach; so I am not looking for anyone to try and re-create this error for me. I am just looking for some feedback on how I could address this error.

KeyError                                  Traceback (most recent call last)
C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance)
   1944             try:
-> 1945                 return self._engine.get_loc(key)
   1946             except KeyError:

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)()

KeyError: 'predictedY'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py in set(self, item, value, check)
   3414         try:
-> 3415             loc = self.items.get_loc(item)
   3416         except KeyError:

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance)
   1946             except KeyError:
-> 1947                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   1948 

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)()

KeyError: 'predictedY'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-95-476dc59cd7fa> in <module>()
     26     return gp, results
     27 
---> 28 gp_dailyElectricity, results_dailyElectricity = predictAll(3, 0.04, trainX_dailyElectricity, trainY_dailyElectricity, testX_dailyElectricity, testY_dailyElectricity, testSet_dailyElectricity, 'Daily Electricity')

<ipython-input-95-476dc59cd7fa> in predictAll(theta, nugget, trainX, trainY, testX, testY, testSet, title)
      8 
      9     results = testSet.copy()
---> 10     results['predictedY'] = predictedY
     11     results['sigma'] = sigma
     12 

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   2355         else:
   2356             # set column
-> 2357             self._set_item(key, value)
   2358 
   2359     def _setitem_slice(self, key, value):

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
   2422         self._ensure_valid_index(value)
   2423         value = self._sanitize_column(key, value)
-> 2424         NDFrame._set_item(self, key, value)
   2425 
   2426         # check if we are modifying a copy

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py in _set_item(self, key, value)
   1462 
   1463     def _set_item(self, key, value):
-> 1464         self._data.set(key, value)
   1465         self._clear_item_cache()
   1466 

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py in set(self, item, value, check)
   3416         except KeyError:
   3417             # This item wasn't present, just insert at end
-> 3418             self.insert(len(self.items), item, value)
   3419             return
   3420 

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py in insert(self, loc, item, value, allow_duplicates)
   3517 
   3518         block = make_block(values=value, ndim=self.ndim,
-> 3519                            placement=slice(loc, loc + 1))
   3520 
   3521         for blkno, count in _fast_count_smallints(self._blknos[loc:]):

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py in make_block(values, placement, klass, ndim, dtype, fastpath)
   2516                      placement=placement, dtype=dtype)
   2517 
-> 2518     return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
   2519 
   2520 # TODO: flexible with index=None and/or items=None

C:\Users\brennn1\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py in __init__(self, values, placement, ndim, fastpath)
     88             raise ValueError('Wrong number of items passed %d, placement '
     89                              'implies %d' % (len(self.values),
---> 90                                              len(self.mgr_locs)))
     91 
     92     @property

ValueError: Wrong number of items passed 3, placement implies 1

My code is as follows:

def predictAll(theta, nugget, trainX, trainY, testX, testY, testSet, title):

    gp = gaussian_process.GaussianProcess(theta0=theta, nugget =nugget)
    gp.fit(trainX, trainY)

    predictedY, MSE = gp.predict(testX, eval_MSE = True)
    sigma = np.sqrt(MSE)

    results = testSet.copy()
    results['predictedY'] = predictedY
    results['sigma'] = sigma

    print ("Train score R2:", gp.score(trainX, trainY))
    print ("Test score R2:", sklearn.metrics.r2_score(testY, predictedY))

    plt.figure(figsize = (9,8))
    plt.scatter(testY, predictedY)
    plt.plot([min(testY), max(testY)], [min(testY), max(testY)], 'r')
    plt.xlim([min(testY), max(testY)])
    plt.ylim([min(testY), max(testY)])
    plt.title('Predicted vs. observed: ' + title)
    plt.xlabel('Observed')
    plt.ylabel('Predicted')
    plt.show()

    return gp, results

gp_dailyElectricity, results_dailyElectricity = predictAll(3, 0.04, trainX_dailyElectricity, trainY_dailyElectricity, testX_dailyElectricity, testY_dailyElectricity, testSet_dailyElectricity, 'Daily Electricity')
Gary
  • 2,137
  • 3
  • 23
  • 41
  • 1
    I don't know Pandas (you should add that tag, by the way), but that traceback is reporting a `KeyError` on the `results['predictedY'] = ...` line, meaning `results` doesn't have a `'predictedY'` key. Is `results` some Pandas object that allows `[]` access, but doesn't allow you to create new keys? – Kevin J. Chase Apr 04 '17 at 04:09
  • shouldn't results rather be testX.copy(). Could you say how testSet, testX and testY are related? – Quickbeam2k1 Apr 04 '17 at 13:55
  • Not the case for this question, but this is where googling the error led me so I'll add it here. I'd managed to end up with two columns with the same name and was getting the error `ValueError: Wrong number of items passed 2, placement implies 1`. Removing the accidentally-duplicated column solved the problem. – Jamie Bull Jan 08 '21 at 15:25
  • Another idea: this post https://stackoverflow.com/questions/37092187/python-pandas-dataframe-for-tuples says you can change the DF column type to "object" to store tuples in a single DF cell. – travelingbones Apr 09 '21 at 00:54

8 Answers8

87

In general, the error ValueError: Wrong number of items passed 3, placement implies 1 suggests that you are attempting to put too many pigeons in too few pigeonholes. In this case, the value on the right of the equation

results['predictedY'] = predictedY

is trying to put 3 "things" into a container that allows only one. Because the left side is a dataframe column, and can accept multiple items on that (column) dimension, you should see that there are too many items on another dimension.

Here, it appears you are using sklearn for modeling, which is where gaussian_process.GaussianProcess() is coming from (I'm guessing, but correct me and revise the question if this is wrong).

Now, you generate predicted values for y here:

predictedY, MSE = gp.predict(testX, eval_MSE = True)

However, as we can see from the documentation for GaussianProcess, predict() returns two items. The first is y, which is array-like (emphasis mine). That means that it can have more than one dimension, or, to be concrete for thick headed people like me, it can have more than one column -- see that it can return (n_samples, n_targets) which, depending on testX, could be (1000, 3) (just to pick numbers). Thus, your predictedY might have 3 columns.

If so, when you try to put something with three "columns" into a single dataframe column, you are passing 3 items where only 1 would fit.

Savage Henry
  • 1,990
  • 3
  • 21
  • 29
  • 4
    Generalizing away from this one sklearn module, it seems like a good troubleshooting strategy would be: **Use the shape() method** to examine both sides and deduce why your right side won't fit in your left side. Then you'll have important info to help troubleshoot why the shape of one of them is unexpected. – Philip Jan 25 '19 at 23:30
  • 1
    Yes, thank you @Philip . Using the shape method helped. For me the offending item was a empty data frame. – le_llama Mar 31 '20 at 13:33
52

Not sure if this is relevant to your question but it might be relevant to someone else in the future: I had a similar error. Turned out that the df was empty (had zero rows) and that is what was causing the error in my command.

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
  • 4
    similar with me, so i put `if len(df)>0:`, to prevent error – Wahyu Bram Sep 13 '20 at 14:03
  • 1
    This was the answer for me too. I had done `df_splits = np.array_split(input_df, nb_splits)` and the `nb_splits` turned out to be too high for the size of my `input_df` which caused some of the splits to be empty dataframe. – Bikash Gyawali Sep 22 '20 at 10:08
  • Similar with me! This error might occur if you have selected "some rows" of a dataframe and conduct data manipulation on it. When the shape of the selected dataframe is (0, x), this error occurs – Bright Chang Jun 11 '21 at 03:04
  • The op can also try `results['predictedY'] = 0.0` before `results['predictedY'] = predictedY`. It worked for me in order to prevent the error in some cases, but yeah, the dataset was empty when I got this message. – Flavio Rangel Jun 10 '22 at 16:29
3

Another cause of this error is when you apply a function on a DataFrame where there are two columns with the same name.

Minions
  • 5,104
  • 5
  • 50
  • 91
  • Two columns that you are trying to set or used as input parameter for the given function? – Tom S Jul 02 '21 at 09:28
  • @TomS, no, when you have two columns in the DataFrame with the same name. – Minions Jul 02 '21 at 15:01
  • 1
    Thanks, actually already found the issue and it was exactly that. – Tom S Jul 03 '21 at 16:03
  • I encountered this error when I tried to group-by and aggregate using max or mean. Count worked for some reason. After I checked the duplicated columns everything forked fine again. – user3411517 Aug 19 '21 at 08:47
2

So ValueError: The wrong number of items passed 3, placement implies 1 occurs when you're passing to many arguments but method supports only a few. for example -

df['First_Name', 'Last_Name'] = df['Full_col'].str.split(' ', expand = True)

In the above code, I'm trying to split Full_col into two sub-columns names as -First_Name & Last_Name, so here I'll get the error because instead list of columns the columns I'm passing only a single argument.

So to avoid this - use another sub-list

df[['First_Name', 'Last_Name']] = df['Full_col'].str.split(' ', expand = True)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

Starting with pandas 1.3.x it's not allowed to fill objects (e.g. like an eagertensor from an embedding) into columns.

https://github.com/pandas-dev/pandas/blame/master/pandas/core/internals/blocks.py

mr_jack
  • 21
  • 1
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Trenton McKinney Aug 27 '21 at 16:13
  • And the link is dead now. Would be very interested in the difference between pandas 1.3 and older versions. – buhtz Feb 16 '22 at 10:51
0

Just adding this as an answer: nesting methods and misplacing closed brackets will also throw this error, ex:

march15_totals= march15_t.assign(sum_march15_t=march15_t[{"2021-03-15","2021-03-16","2021-03-17","2021-03-18","2021-03-19","2021-03-20","2021-03-21"}]).sum(axis=1)

Versus the (correct) version: march15_totals= march15_t.assign(sum_march15_t=march15_t[{"2021-03-15","2021-03-16","2021-03-17","2021-03-18","2021-03-19","2021-03-20","2021-03-21"}].sum(axis=1))

This is probably common sense to most of you but I was quite puzzled until I realized my mistake.

kmck339
  • 3
  • 2
0

I got this error when I was trying to convert a one-column dataframe, df, into a Series, pd.Series(df). I resolved this with

pd.Series(df.values.flatten())

The problem was that the values in the dataframe were lists:

  my_col
0 ['a']
1 ['b']
2 ['c']
3 ['d']

When I was printing the dataframe it wasn't showing the brackets which made it hard to track down.

Nic Scozzaro
  • 6,651
  • 3
  • 42
  • 46
-8
for i in range(100):
try:
  #Your code here
  break
except:
  continue

This one worked for me.

Frightera
  • 4,773
  • 2
  • 13
  • 28