AllenDowney / ThinkStats2

Text and supporting code for Think Stats, 2nd Edition
http://allendowney.github.io/ThinkStats2/
GNU General Public License v3.0
4.03k stars 11.31k forks source link

_DictWrapper class invokes pandas Series method not present in Py27 when updating data #79

Closed gbremer closed 7 years ago

gbremer commented 7 years ago

in the chap03ex notebook, cell 3 fails when the _DictWrapper class updates data from a pandas Series when running with Python 2.7. The class uses the items() method, which is not present in the 0.20.3 Python 2.7 release of pandas.

The code in cell 3 --

hist = thinkstats2.Hist(live.birthwgt_lb, label='birthwgt_lb')
thinkplot.Hist(hist)
thinkplot.Config(xlabel='Birth weight (pounds)', ylabel='Count')

The exception --

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-1500ad553f53> in <module>()
----> 1 hist = thinkstats2.Hist(live.birthwgt_lb, label='birthwgt_lb')
      2 thinkplot.Hist(hist)
      3 thinkplot.Config(xlabel='Birth weight (pounds)', ylabel='Count')

/Users/gbremer/projects/ThinkStats2/code/thinkstats2.pyc in __init__(self, obj, label)
    153             self.d.update(obj.Items())
    154         elif isinstance(obj, pandas.Series):
--> 155             self.d.update(obj.value_counts().items())
    156         else:
    157             # finally, treat it like a list

/Users/gbremer/anaconda/envs/thinkstats2-27/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
   3075         if (name in self._internal_names_set or name in self._metadata or
   3076                 name in self._accessors):
-> 3077             return object.__getattribute__(self, name)
   3078         else:
   3079             if name in self._info_axis:

AttributeError: 'Series' object has no attribute 'items'

The Series.value_counts() object, a Series instance, does have an iteritems() method which works.

The code works in Python 3.6 using both the items() and iteritems() method; items() and iteritems() are both implemented.

Looking at the pandas docs, items() and iteritems() are equivalent --

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html

items() | Lazily iterate over (index, value) tuples iteritems() | Lazily iterate over (index, value) tuples

gbremer commented 7 years ago

Fixed raised and merged