1
def dailyTimeDistributionFeatures ( dailyCallDistribution_dictionary, missingValue = -999, lowSampleValue = -666, numberOfFeatures = 14, sampleSizeThreshold = 3 ):     
     featureSelection = {}
     for date in dailyCallDistribution_dictionary:
          date_timestruct = datetime.datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d")))
          timeSample = dailyCallDistribution_dictionary[ date ]
          if len(timeSample) <= sampleSizeThreshold :
            if len(timeSample) == 0 :
               featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday
                                          , int(date_timestruct.strftime('%W'))
                                          , date_timestruct.month ] + [missingValue] * (numberOfFeatures - 3)
            else :
               featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday
                                          , int(date_timestruct.strftime('%W'))
                                          , date_timestruct.month ] + [lowSampleValue] * (numberOfFeatures - 3)
          else :    
               featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday
                                          , int(date_timestruct.strftime('%W'))
                                          , date_timestruct.month
                                          , len( timeSample )
                                          # counts how many late night activities.
                                          , sum( Pandas.Series( timeSample ).apply( lambda x: (x>0) & (x <= 4) ).tolist() )
                                          , Pandas.Series( timeSample ).mean()
                                          , Pandas.Series( timeSample ).median() 
                                          , Pandas.Series( timeSample ).std() 
                                          , Pandas.Series( timeSample ).min()
                                          , Pandas.Series( timeSample ).max()
                                          , Pandas.Series( timeSample ).mad()
                                          , Pandas.Series( timeSample ).quantile(0.75) - Pandas.Series( timeSample ).quantile(0.25)
                                          , Pandas.Series( timeSample ).kurt() 
                                          , Pandas.Series( timeSample ).skew() 
                                          ]
     return Pandas.DataFrame( featureSelection, index = ['dayOfWeek', 'WeekOfYear', 'MonthOfYear', 
                                                         'Number of Calls', 'Number of Late Night Activities', 
                                                         'Average Time', 'Median of Time',
                                                         'Standard Deviation', 'Earliest Call',
                                                         'Latest Call', 'Mean Absolute Deviation',
                                                         'Interquartile Range', 'Kurtosis',
                                                         'Skewness'] ).T

When I wrote the above python function that outputs a data frame and attempt to add one more column to the data frame within the above function:

featureTime['Whether Staying Late'] = featureTime['Number of Late Night Activities'].apply( lambda x: x > 0 ).apply(lambda x: sum(x))

I got an error:

TypeError                                 Traceback (most recent call last)
/home/aaa/Enthought/Canopy_64bit/System/lib/python2.7/site- packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
181             else:
182                 filename = fname
--> 183             __builtin__.execfile(filename, *where)

/home/aaa/pyRepo/feature_selection_v15.py in <module>()
352 featureTime.to_csv('time.csv')
353 
--> 354 featureTime['Whether Staying Late'] = featureTime['Number of Late Night    Activities'].apply( lambda x: x > 0 ).apply(lambda x: sum(x))
355 
356 

/home/aaa/Enthought/Canopy_64bit/User/lib/python2.7/site- packages/pandas/core/series.pyc in apply(self, func, convert_dtype, args, **kwds)
2445             values = lib.map_infer(values, lib.Timestamp)
2446 
-> 2447         mapped = lib.map_infer(values, f, convert=convert_dtype)
2448         if isinstance(mapped[0], Series):
2449             from pandas.core.frame import DataFrame

/home/aaa/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/lib.so in pandas.lib.map_infer (pandas/lib.c:41822)()

/home/aaa/pyRepo/feature_selection_v15.py in <lambda>(x)
352 featureTime.to_csv('time.csv')
353 
--> 354 featureTime['Whether Staying Late'] = featureTime['Number of Late Night  Activities'].apply( lambda x: x > 0 ).apply(lambda x: sum(x))
355 
356 

TypeError: 'numpy.bool_' object is not iterable

which does not exist, if I add it manually by talking to the console.

I have resolved the issue by working with python built-in data types and a for loop instead. What makes me curious is why I am getting the kind of bug above... Want to know where it comes from...

1
  • can you post the full stack trace? Commented Jul 4, 2013 at 19:04

1 Answer 1

1

Assuming sequence.apply applies the lambda to each element in the sequence, sequence.apply(lambda x: x > 0) yields a sequence of Boolean values, and sequence.apply(lambda x: x > 0).apply(lambda x: sum(x)) attempts to sum each Boolean value, resulting in a 'bool' object is not iterable-kinda error. You get a similar error from:

>>> sum(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not iterable
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm very bizarre, sum(True) actually works for me (not sum([True]), however both work on my pc). And what bothers sme further is that using exactly the same code, in one case, I put it into the .py file, and it doesnt work; in another, i typed it into the console and it works well... Maybe it is my python/linux version.
I used another IDE, sum(True) now returns an error. I think canopy does the "correction" for me. Maybe the other weird things come from this IDE too... Thanks a lot for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.