Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float Series into an integer Series in pandas

I have the following data frame:

In [31]: rise_p
Out[31]: 
         time    magnitude
0  1379945444   156.627598
1  1379945447  1474.648726
2  1379945448  1477.448999
3  1379945449  1474.886202
4  1379945699  1371.454224

Now, I want to group rows which are within a minute. So I divide the time series with 100. I get this:

In [32]: rise_p/100
Out[32]: 
          time  magnitude
0  13799454.44   1.566276
1  13799454.47  14.746487
2  13799454.48  14.774490
3  13799454.49  14.748862
4  13799456.99  13.714542

As explained above, I want to create groups based on time. So expected subgroups would be rows with times 13799454 and 13799456. I do this:

In [37]: ts = rise_p['time']/100

In [38]: s = rise_p/100

In [39]: new_re_df = [s.iloc[np.where(int(ts) == int(i))] for i in ts]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-5ea498cf32b2> in <module>()
----> 1 new_re_df = [s.iloc[np.where(int(ts) == int(i))] for i in ts]

TypeError: only length-1 arrays can be converted to Python scalars

How do I convert ts into an Integer Series since int() doesn't take a Series or a list as an argument? Is there any method in pandas which does this?

like image 425
Geekster Avatar asked Sep 26 '13 11:09

Geekster


1 Answers

Try converting with astype:

new_re_df = [s.iloc[np.where(ts.astype(int) == int(i))] for i in ts]

Edit

On suggestion by @Rutger Kassies a nicer way would be to cast series and then groupby:

rise_p['ts'] = (rise_p.time / 100).astype('int')

ts_grouped = rise_p.groupby('ts')

...
like image 76
C Mars Avatar answered Sep 27 '22 19:09

C Mars