title: Counting the number of results for a pandas.loc search
body: <p>I have a pandas DataFrame with two columns "user" (userid) and "TS" (timestamp).</p>
  
  <pre><code>&gt;&gt;&gt; print rawData
  &lt;class 'pandas.core.frame.DataFrame'&gt;
  Int64Index: 74883 entries, 0 to 74882
  Data columns (total 2 columns):
  TS      74883  non-null values
  user    74883  non-null values
  dtypes: float64(1), object(1)
  </code></pre>
  
  <p>I've ensured that the dataframe is indexed on the 'user' field.</p>
  
  <pre><code>&gt;&gt;&gt; indexedDataFrame = rawData.set_index('user')
  </code></pre>
  
  <p>So that now I can get the list of timestamps for a user.</p>
  
  <pre><code>&gt;&gt;&gt; indexedDataFrame.loc['user1','TS']
  user
  user1   1423217959.064690
  user1   1423217959.204906
  user1   1423217959.324809
  ...
  user1   1423220404.366348
  user1   1423221013.064749
  Name: TS, Length: 3151, dtype: float64
  </code></pre>
  
  <p>This allows me to find the number of entries for this user by counting the
  number of entries in the array.</p>
  
  <pre><code>&gt;&gt;&gt; indexedDataFrame.loc['user1', 'TS'].count()
  3151
  </code></pre>
  
  <p>So far so good. The problem occurs when there is only a single entry for a
  user. In that case, pandas, presumably to make it easier for users, returns the
  entry directly instead of an array with one value. Unfortunately, this means that the count operation
  fails since you can't call count() on a float.</p>
  
  <pre><code>&gt;&gt;&gt; indexedDataFrame.loc['user15', 'TS']
  1423217968.5711029
  
  &gt;&gt;&gt; indexedDataFrame.loc['user15', 'TS'].count()
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-259-8712c5fa66de&gt; in &lt;module&gt;()
  ----&gt; 1 indexedDataFrame.loc['user15', 'TS'].count()
  
  AttributeError: 'numpy.float64' object has no attribute 'count'
  </code></pre>
  
  <p>So this means that if I want to write a function that counts the number of
  timestamps for each user, I have to do the following:</p>
  
  <pre><code>def findEventCounts():
      userCountMap = {}
      for user in distinctUsers:
          print "Counting entries for user %s" % user
          searchResult = indexedDataFrame.loc[user, 'TS']
          try:
              userCount = searchResult.count()
          except AttributeError:
              userCount = 1
          print user, userCount
          userCountMap[user] = userCount
      return userCountMap
  </code></pre>
  
  <p>which is stupid and ugly.</p>
  
  <p>Unfortunately, this behavior of returning the value instead of an array with a
  single entry actually shows up in the pandas documentation as well, so I'm
  pretty sure that this is expected behavior.</p>
  
  <p>(See the last example in <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-label" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-label</a>, </p>
  
  <pre><code># this is also equivalent to ``df1.at['a','A']``
  In [44]: df1.loc['a','A']
  Out[44]: -1.7769037169718671
  </code></pre>
  
  <p>)</p>
  
  <p>Is there a workaround, or do I really have to write ugly code like the above to
  use pandas? If it is the latter, I'm not going to use pandas... :)</p>