Convert Float series to whole 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 the rows that are in a minute. Therefore, I divide the time series by 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 time groups. Therefore, the expected subgroups will be lines with the time 13799454 and 13799456 . I'm doing it:

 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 to convert ts to a whole chain since int () does not accept a series or list as an argument? Is there any method in pandas that does this?

+8
python pandas time-series
source share
2 answers

Try converting with astype:

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

Edit

At the suggestion of @Rutger Kassies, the best way would be to drop the series and then groupby:

 rise_p['ts'] = (rise_p.time / 100).astype('int') ts_grouped = rise_p.groupby('ts') ... 
+13
source share

Here is another way to solve your problem.

 In [3]: df Out[3]: time magnitude 0 1379945444 156.627598 1 1379945447 1474.648726 2 1379945448 1477.448999 3 1379945449 1474.886202 4 1379945699 1371.454224 In [4]: df.dtypes Out[4]: time int64 magnitude float64 dtype: object 

Convert timestamps in seconds

 In [7]: df['time'] = pd.to_datetime(df['time'],unit='s') 

Set index

 In [8]: df.set_index('time',inplace=True) In [9]: df Out[9]: magnitude time 2013-09-23 14:10:44 156.627598 2013-09-23 14:10:47 1474.648726 2013-09-23 14:10:48 1477.448999 2013-09-23 14:10:49 1474.886202 2013-09-23 14:14:59 1371.454224 

Grouping by 1min means results ( how= can also be an arbitrary function)

 In [10]: df.resample('1Min',how=np.mean) Out[10]: magnitude time 2013-09-23 14:10:00 1145.902881 2013-09-23 14:11:00 NaN 2013-09-23 14:12:00 NaN 2013-09-23 14:13:00 NaN 2013-09-23 14:14:00 1371.454224 
+4
source share

All Articles