I'm having trouble with pandas (v0.11 or v0.12) groupby code returning a different structure of output when my input series has exactly one record. I use a fixed set of bins, and count the number of occurrences in each bucket, based on code similar to below.
s0 = pd.Series([1,2,1,3,1,4,1,2,1], name='foo')
s = s0
#s = s0[:0]
#s = s0[:1]
#s = s0[:2]
bins = pd.cut(s,[0,2,4])
s.groupby(bins).count()
For any length of input series (except 1), I get an output series with one record per bin along with a count for that bin, even if it's zero. Something like this:
foo
(0, 2] 7
(2, 4] 2
dtype: int64
But if the input series has length 1, I get output with a different structure, like this:
((0, 2]) 1
dtype: int64
I'm wondering if this is related to the problem I often run into where a single-record slice of a dataframe turns into a series representing the row values, instead of a single-record dataframe. (Which you can avoid using df.ix[n:n] instead of df.ix[n].)
But I don't see how to work around it easily here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…