[Tutor] error message

2009-07-08 Thread Steven Buck
I'm running a for loop which returns an error message after the third
iteration (see out[4] at the bottom as evidence).  I don't understand the
error message.  Although I'll continue to do my own digging to debug, I
thought I'd give you all a shot.  Thanks, -steve

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
Type copyright, credits or license for more information.
IPython 0.9.1 -- An enhanced Interactive Python.
? - Introduction and overview of IPython's features.
%quickref - Quick reference.
help  - Python's own help system.
object?   - Details about 'object'. ?object also works, ?? prints more.
In [1]: import psid
In [2]: age = {}
In [3]: for i in range(len(psid.psid)):
   ...: age[(i)] = psid.psid[i][20]
   ...:
   ...:
---
error Traceback (most recent call last)
C:\Documents and Settings\Steve\ipython console in module()
C:\Python26\lib\site-packages\StataTools.pyc in __getitem__(self, k)
 85 if self._file.tell() != loc:
 86 self._file.seek(loc)
--- 87 return self._next()
 88
 89 ### PyDTA private methods

C:\Python26\lib\site-packages\StataTools.pyc in _next(self)
167 else:
168 data[i] = self._unpack(typlist[i],
self._file.read(s
elf._col_size(i)))
169 return data
170 else:
-- 171 return map(lambda i: self._unpack(typlist[i],
self._file.rea
d(self._col_size(i))), range(self._header['nvar']))
C:\Python26\lib\site-packages\StataTools.pyc in lambda(i)
167 else:
168 data[i] = self._unpack(typlist[i],
self._file.read(s
elf._col_size(i)))
169 return data
170 else:
-- 171 return map(lambda i: self._unpack(typlist[i],
self._file.rea
d(self._col_size(i))), range(self._header['nvar']))
C:\Python26\lib\site-packages\StataTools.pyc in _unpack(self, fmt, byt)
148
149 def _unpack(self, fmt, byt):
-- 150 d = unpack(self._header['byteorder']+fmt, byt)[0]
151 if fmt[-1] in self.MISSING_VALUES:
152 nmin, nmax = self.MISSING_VALUES[fmt[-1]]
error: unpack requires a string argument of length 1
In [4]: age
Out[4]: {0: 22, 1: 51, 2: 42}
In [5]:
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] error message

2009-07-08 Thread Steven Buck
As Bob prescribed, I added (and made sure to indent):

print self._header['byteorder'], fmt, byt

The fourth printed line appears to be the same:
Out[4]: {0: 22, 1: 51, 2: 42}

This is consistent with what I observe as the first three age observations
in the Stata data editor.

I include the rest of the error message I received ( could capture) below.
Any more thoughts?
Thanks
Steve

 b ▬
 b §
 b ☺
 b ☺
 b ☺
 b ☻
 b ☺
 f  PCF
 b ♣
 f ∞Qê@
 b ☺
 f   `A
 b ☺
 b ♣
 f
 f
 b
 b
 b ♣
 h ╥
 f   ßC
 b e
 b ♣
 b
 b
 b
 b ☺
 b ♥
 b ♥
 b ☺
 h ñ
 h
 f 5î─D
 b ☺
 b
 b
 b
 b
 b
 b ☺
 b ☺
 h Σ☺
 b ☺
 b ☺
 f   ÉB
 f ÜÖôB
 f ÜÖêB
 f   âB
 f ═╠ƒB
 f ÜÖàB
 f ÜÖúB
 f ffzB
 f ═╠ùB
 f fféB
 f ffrB
 f 33æB
 f ∞Q8?
 f ü↓ⁿ@
 f «1¿╛
 f b♥2A
 f �...@a
 f ♠·OA
 f ╟▌QA
 f   Ç?
 h ⌠←
 b
 h ║
 h ☻
 f  áîF
 f
 h (♣
 f   èE
 h
 h ╝
 h
 f  α½F
 f
 f  α½F
 f   ╥E
 f  hαF
 f   }E
 b 
 b ☺
 b ☻
 b 3
 b 1
 b ☺
 b ☻
 b ☺
 b ☺
 b ☺
 f  α½F
 b e
 f Å┬1A
 b
 f   ,B
 b ♣
 b ♣
 f
 f
 b
 b ☺
 b ♣
 h
 f
 b e
 b ☺
 b ☺
 b
 b
 b ☻
 b ☺
 b ☺
 b ☻
 h ç
 h
 f 8↑sE
 b
 b ☺
 b
 b
 b ☺
 b
 b
 b
 h )
 b ☺
 b
 f   ÉB
 f ÜÖôB
 f ÜÖêB
 f   âB
 f ═╠ƒB
 f ÜÖàB
 f ÜÖúB
 f ffzB
 f ═╠ùB
 f fféB
 f ffrB
 f 33æB
 f ∞Q8?
 f ≥Ä♂A
 f «1¿╛
 f ░₧CA
 f _iXA
 f ↑├`A
 f ♫»iA
 f   Ç@
 h àD
 b ☺
 h ║
 h Ñ♥
 f
 f  Ç╨D
 h
 f   ☻E
 h
 h ╚♠
 h
 f  @£E
 f
 f  @£E
 f
 f  @£E
 f
 b
 b ☻
 b ☻
 b *
 b )
 b ☺
 b
 b ☺
 b ☺
 b ☺
 f  @£E
 b ☺
 f ∞Q8@
 b
 f   Ç?
 b ☺
 b ♣
 f
 f
 b
 b
 b ♣
 h
 f
 b e
 b ♣
 b
 b
 b
 b ☻
 b ☻
 b ☻
 b ♥
 h É
 h
 f ^ë↑D
 b
 b
 b ☺
 b
 b
 b ☺
 b
 b
 h Σ♠
 b ☺
 b
 f   ÉB
 f ÜÖôB
 f ÜÖêB
 f   âB
 f ═╠ƒB
 f ÜÖàB
 f ÜÖúB
 f ffzB
 f ═╠ùB
 f fféB
 f ffrB
 f 33æB
 f ∞Q8?
 f ■■■@
 f «1¿╛
 f ä▐3A
 f :─CA
 f ¡♫RA
 f bVUA
 f   @@
 h ▐►
 b
 h ║
 h ►►
 f
 f   aD
 h X☻
 f  ÇE
 h
 h ÷
 h ≡
 f  ╝TF
 f   HD
 f  αvF
 f
 f  αvF
 f   »D
 b ↑
 b ♣
 b ♣
 b
---
error Traceback (most recent call last)
C:\Documents and Settings\Steve\ipython console in module()
C:\Python26\lib\site-packages\StataTools.py in __getitem__(self, k)
 85 if self._file.tell() != loc:
 86 self._file.seek(loc)
--- 87 return self._next()
 88
 89 ### PyDTA private methods

C:\Python26\lib\site-packages\StataTools.py in _next(self)
168 else:
169 data[i] = self._unpack(typlist[i],
self._file.read(s
elf._col_size(i)))
170 return data
171 else:
-- 172 return map(lambda i: self._unpack(typlist[i],
self._file.rea
d(self._col_size(i))), range(self._header['nvar']))
C:\Python26\lib\site-packages\StataTools.py in lambda(i)
168 else:
169 data[i] = self._unpack(typlist[i],
self._file.read(s
elf._col_size(i)))
170 return data
171 else:
-- 172 return map(lambda i: self._unpack(typlist[i],
self._file.rea
d(self._col_size(i))), range(self._header['nvar']))
C:\Python26\lib\site-packages\StataTools.py in _unpack(self, fmt, byt)
149 def _unpack(self, fmt, byt):
150 print self._header['byteorder'], fmt, byt
-- 151 d = unpack(self._header['byteorder']+fmt, byt)[0]
152 if fmt[-1] in self.MISSING_VALUES:
153 nmin, nmax = self.MISSING_VALUES[fmt[-1]]
error: unpack requires a string argument of length 1
In [4]: age
Out[4]: {0: 22, 1: 51, 2: 42}
In [5]:


On Wed, Jul 8, 2009 at 1:12 PM, bob gailer bgai...@gmail.com wrote:

   Steven Buck wrote:

 I'm running a for loop which returns an error message after the third
 iteration (see out[4] at the bottom as evidence).  I don't understand the
 error message.  Although I'll continue to do my own digging to debug, I
 thought I'd give you all a shot.  Thanks, -steve

 Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit
 (Intel)]
 Type copyright, credits or license for more information.
 IPython 0.9.1 -- An enhanced Interactive Python.
 ? - Introduction and overview of IPython's features.
 %quickref - Quick reference.
 help  - Python's own help system.
 object?   - Details about 'object'. ?object also works, ?? prints more.
 In [1]: import psid
 In [2]: age = {}
 In [3]: for i in range(len(psid.psid)):
...: age[(i)] = psid.psid[i][20]
...:
...:
 ---
 error Traceback (most recent call last)
 C:\Documents and Settings\Steve\ipython console in module()
 C:\Python26\lib\site-packages\StataTools.pyc in __getitem__(self, k)
  85 if self._file.tell() != loc:
  86 self._file.seek(loc)
 --- 87 return self._next()
  88
  89 ### PyDTA private methods

 C:\Python26\lib\site-packages\StataTools.pyc in _next(self)
 167 else:
 168 data[i] = self._unpack(typlist[i],
 self._file.read(s
 elf._col_size(i

Re: [Tutor] append question

2009-07-06 Thread Steven Buck
Thanks for the previous responses.  This isn't homework--I'm beyond
coursework, although I am a newbie to Python (and I've never had to do much
real programming since I've just used Stata for econometric analysis).  I'm
testing Python as a more powerful alternative to Stata.

I've learned from the responses I received, although now see my problem
differently.  The data structure I have uses a dictionary and know now
that the append command doesn't work.  Having said that, perhaps my
variables of interest have already been created--perhaps I just don't know
how to identify them.  I've been using some borrowed code to get me started;
my modified version is below:


import sys

# The modules below help me get a .dta file into Python. # Although I'm not
sure what form they take; I suppose a list of lists???
from StataTools import Reader
from StataTypes import MissingValue

# I call my data set the psid (Panel Study of Income Dynamics) # In Stata
this would look like and NXK matrix (N observations and K variables)
psid=Reader(file('data3.dta'))

# I gather this next just creates a list of the variable names. varnames=[
x.name for x in psid.variables()]

# It's not clear what these next two lines gain me.
labels=psid.file_headers()['vlblist']
Labels=dict(zip(varnames,labels))


 From here, I'd like Python to identify the Nx1 vectors (or n-tuples) that
correspond to the varnames list defined above.  I can't seem grab the
vectors representing age, wage, etc..  I've tried things like
age, psid['age'], psid.age.  My last email was an attempt to create the
vectors myself, although the Reader module puts the data in a dictionary
structure so the append command I was trying to use doesn't work.

Hopefully once I learn to create and call on my own vectors and matrices
I'll be better off--I'm comfortable working with these in MATLAB and Stata.

Bottom line:  Given the above data I've imported/extracted from Stata .dta
file, how do I create an Nx1 vector  which I call 'age'?

Thanks for your patience with this newbie.
Steve




On Sun, Jul 5, 2009 at 5:19 PM, Rich Lovely roadier...@googlemail.comwrote:


 2009/7/5 Steven Buck bucks...@gmail.com:
  for i in len(test):  testvar2.append(test[i][2])
 
  I want testvar2 = [2,5,8] but instead I get the following error message:
 
  Traceback (most recent call last):
File pyshell#34, line 1, in module
  for i in len(test):
  TypeError: 'int' object is not iterable
 
  Any insight would be appreciated.
  Thanks
  Steve
  --
  Steven Buck
  Ph.D. Student
  Department of Agricultural and Resource Economics
  University of California, Berkeley


 This sounds like a homework assignment, and we're not supposed to give out
 answers to homework.

 The error message and the docs explain what you're doing wrong if you
 take a moment to look.
 from http://www.python.org/doc/2.6/reference/compound_stmts.html#for

 for_stmt ::=  for target_list in expression_list : suite
  [else : suite]

 The expression list is evaluated once; it should yield an iterable
 object. An iterator is created for the result of the expression_list.
 The suite is then executed once for each item provided by the
 iterator, in the order of ascending indices. Each item in turn is
 assigned to the target list using the standard rules for assignments,
 and then the suite is executed.

 As Luke said, len returns an int, which as your error tells you, is
 not iterable.  From the same page:
 The for statement is used to iterate over the elements of a
 sequence (such as a string, tuple or list) or other iterable
 object:

 Therefore you have an iterable, there is no need to try and construct a new
 one.

 Does that help?

 It is extremly unpythonic to iterate over range(len(...)), as it adds
 in the overhead of two function calls, and ruins the readability of
 code.  The latter is probably the most important of the two.

 An even more pythonic way to do this would be a list comprehension,

 http://www.python.org/doc/2.6/tutorial/datastructures.html#list-comprehensions

 If it's not homework, let us know, and we'll be more than willing to
 give you code if you still need it.

 --
 Richard Roadie Rich Lovely, part of the JNP|UK Famile
 www.theJNP.com http://www.thejnp.com/


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] append question

2009-07-05 Thread Steven Buck
Hi Python Tutors:

I have a data structure that looks like:

 test=[[1,2,3],[4,5,6],[7,8,9]]

I want to define a new variable that captures the second element of each
sublist from above:

 testvar2 = []

Next I try to capture the aforementioned elements:

 for i in len(test):
testvar2.append(test[i][2])

I want testvar2 = [2,5,8] but instead I get the following error message:

Traceback (most recent call last):
  File pyshell#34, line 1, in module
for i in len(test):
TypeError: 'int' object is not iterable

Any insight would be appreciated.
Thanks
Steve








-- 
Steven Buck
Ph.D. Student
Department of Agricultural and Resource Economics
University of California, Berkeley
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] reading variables in a data set?

2009-07-04 Thread Steven Buck
 Dear Python Tutor,
I'm doing econometric work and am a new user of Python. I have read several
of the tutorials, but haven't found them useful for a newbie problem I've
encountered.
I've used a module (StataTools) from (http://presbrey.mit.edu/PyDTA ) to get
a Stata .dta file into Python. In Stata the data set is an NXK matrix
where N is the number of observations (households) and K is the number of
variables.
I gather it's now a list where each element of the list is an observation (a
vector) for one household.  The name of my list is data; I gather Python
recognizes the first observation by: data[1] .
Example,
data = [X_1, X_2, X_3, . . . . , X_N]  where each X_i for all i, is vector
of household characteristics, eg X_1 = (age_1, wage_1, . . . , residence_1).

I also have a list for variable names called varname; although I'm not
sure the module I used to extract the .dta into Python also created a
correspondence between the varname list and the data list--the python
interpreter won't print anything when I type one of the variable names, I
was hoping it would print out a vector of ages or the like.

In anycase, I'd like to make a scatter plot in pylab, but don't know how to
identify a variable in data (i.e.  I'd like a vector listing the ages and
another vector listing the wages of  households).  Perhaps, I need to run
subroutine to collect each relevant data point to create a new list which I
define as my variable of interest?  From the above example, I'd like to
create a list such as: age = [age_1, age_2, . . . , age_N] and likewise for
wages.

Any help you could offer would be very much appreciated.  Also, this is my
first time using the python tutor, so let me know if I've used it
appropriately or if I should change/narrow the structure of my question.

Thanks
Steve

-- 
Steven Buck
Ph.D. Student
Department of Agricultural and Resource Economics
University of California, Berkeley
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor