0

The code below breaks on the simplest of lines, where I thought I was just adding a column called 'year' with the constant value of what is in the year variable. My packages are up to date with Anaconda 2.3.

Why is this indexing wrong?

The code:

# -*- coding: utf-8 -*-
import iopro
from pandas import *

for year in xrange(2005,2013):
    for month in xrange(1,13):
        if year == 2005 and month < 7:
            continue
        filename = 'SOMEPATH' + str(year) + '_mon'+ str(month) +'.txt'
        adapter = iopro.text_adapter(filename,parser='csv',field_names=True,output='dataframe',delimiter='\t')
        monthly = adapter[['var1','var2','var3']][:]
        monthly['year']=year

The error message is:

Traceback (most recent call last):
 File "drugs.py", line 21, in <module>
   monthly['year']=year
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2127, in __setitem__
   self._set_item(key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2205, in _set_item
   NDFrame._set_item(self, key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 1196, in _set_item
   self._data.set(key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 2980, in set
   loc = self.items.get_loc(item)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
   return self._engine.get_loc(_values_from_object(key))
 File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)
TypeError: expected string or Unicode object, slice found
asmeurer
  • 86,894
  • 26
  • 169
  • 240
László
  • 3,914
  • 8
  • 34
  • 49
  • 1
    Can you make this into a reproducible example? – chrisb Jul 25 '15 at 14:22
  • @László Can you verify that the output from your call to `iopro.text_adapter` is a `DataFrame` object?? – kennes Jul 25 '15 at 14:38
  • @bleh Not easily at the moment (data is on a remote server). But you saw that I used the option for that, right? If I got a numpy array there, how would the next line work, or in any case, why would I get pandas error messages? – László Jul 25 '15 at 14:46
  • Maybe unrelated, but it looks like there is a quotation mark missing on this line: `filename = 'SOMEPATH + str(year) ` – unutbu Jul 25 '15 at 14:53
  • @HappyLeapSecond Thanks, was a typo in the post, fixed in an edit, does not affect the real code. – László Jul 25 '15 at 15:05
  • 1
    Your syntax looks correct to me. I think you will need to investigate your output from `adapter`. – kennes Jul 25 '15 at 15:15
  • @bleh But what would you investigate? Doesn't the problem look like one with `monthly['year']=year`? What could affect that? Esp. where parsing find a slice for an index? – László Jul 25 '15 at 15:47
  • Have you tried using pd.read_csv? – Alexander Jul 25 '15 at 15:52
  • @László: Start printing things, such as `type(monthly)` (should be DataFrame), `monthly.info()`, `monthly.head()`, `monthly.columns` (check that `'year'` is listed), etc. When things don't make sense, it means something you believe to be obviously true is actually false. – unutbu Jul 25 '15 at 15:53
  • @HappyLeapSecond I am adding the 'year' column in the line that crashes. Á la http://stackoverflow.com/q/24039023/938408 – László Jul 25 '15 at 15:56
  • Yes, I know. It seems impossible. That is why we need to check basic assumptions. One of them is wrong. – unutbu Jul 25 '15 at 15:59
  • 3
    You could also run `python -m pdb /path/to/drugs.py`. This will drop you into the pdb debugger. Press `c` to continue (i.e. start running drugs.py). It will break on the `TypeError`. Then `p key` to print the value of `key`. What slice could this possibly be? Then press `u` to go up a frame. You might press `u` 4 times to get to `self._set_item(key, value)`. Then try `p key` and `p value` to see the values of `key` and `value`. Do they match what you expect? `key` should be the string `'year'` and `value` should be an int, the value of `year`... – unutbu Jul 25 '15 at 16:08

0 Answers0