[Here is how my last data from my dataframe and at the end errors which are occuring]我正在打印特定范围内的数据。例如now + 200 sec
,所以这里我要从现在起200秒内打印数据。
我尝试了while循环,即在日期+200秒内打印数据。但是最后,如果数据时间少于日期+ 200秒 ,则会引发错误“单个位置索引器超出范围”
file = pd.read_hdf('KazSTSAT5.h5', mode = 'r', index_col = 'time')
df = pd.DataFrame (file)
df['time'] = pd.to_datetime(df['time'],unit='s')
initial_size = df.size
while True:
df['time'] = pd.to_datetime(df['time'],unit='s')
pd.set_option('display.max_rows', -1)
#first date in dataframe
first_datetime = df.iloc[1, 1]
#date range 200 sec
i = pd.date_range(first_datetime, periods=200, freq='S')
b = df.loc[(df.time >= i[0]) & (df.time <= i[-1])]
c = pd.concat([df, b, b]).drop_duplicates(keep=False)
size = c.size
initial_size = size
df = c
如果时间少于日期+200秒,如何获得结果?因此,它只需要保存dataleft中的内容即可。
答案 0 :(得分:0)
df2 = df.loc[df.loc[:,'time'] < date+200,:]
答案 1 :(得分:0)
如果您只是想知道,当到达min
-和max
-时间相差不到200秒时,可以执行以下操作:
确定循环必须终止的时间点:
mn= df2['time'].min()
mx= df2['time'].max()
td= mx-mn
if td.seconds < 200:
# do something usefull here to process the rest of data
break # exit the loop
替代方法,如果您想汇总数据
但是我认为,您实际上是在进行某种聚合,如果这样,可能会有更有效的方式来处理数据。
要解决此问题,您可以创建一个具有200秒时隙的日期范围,并将其与merge_asof
合并,以便以后进行分组。
因此,基本思想如下:
# create a date range and convert it to a series
# just make sure, you set the end properly
interval_series= interval_series= pd.date_range(start='2019-09-10 02:18:23', end='2030-08-01 00:00:00', freq='200S').to_series()
interval_series.name= 'interval_time'
# now align that index to your data
# to use the following, you have to make sure,
# your time column is of type datetime64
index_date_df= pd.merge_asof(df[['time']], interval_series, left_on='time', right_index=True)
# now you can use the assigned intrval time,
# which is from the date_range, to group
# your dataframe
# the following example would just take the
# first row within each 200s slot
index_date_df['interval_time']
df.groupby(index_date_df['interval_time']).agg('first').reset_index(drop=True)
输出:
Out[78]:
country time
0 USA 2019-09-10 02:18:23
1 MX 2019-09-10 02:24:48
2 USA 2019-09-10 02:25:34
3 USA 2019-09-10 02:29:07
4 USA 2019-09-10 02:32:21
5 MX 2019-09-10 02:38:36
6 MX 2019-09-10 02:43:34
7 USA 2019-09-10 02:47:09
8 USA 2019-09-10 02:48:48
9 USA 2019-09-10 02:55:56
10 USA 2019-09-10 03:01:33
11 MX 2019-09-10 03:02:09
12 USA 2019-09-10 03:05:42
13 MX 2019-09-10 03:09:42
14 MX 2019-09-10 03:12:39
根据以下测试数据:
import io
raw=\
""" country time
0 USA 2019-09-10 02:18:23
1 USA 2019-09-10 02:19:53
2 MX 2019-09-10 02:24:48
3 USA 2019-09-10 02:25:34
4 USA 2019-09-10 02:29:07
5 MX 2019-09-10 02:29:43
6 USA 2019-09-10 02:32:21
7 USA 2019-09-10 02:34:58
8 MX 2019-09-10 02:38:36
9 MX 2019-09-10 02:39:49
10 MX 2019-09-10 02:43:34
11 MX 2019-09-10 02:44:51
12 USA 2019-09-10 02:47:09
13 USA 2019-09-10 02:48:11
14 USA 2019-09-10 02:48:48
15 MX 2019-09-10 02:51:19
16 USA 2019-09-10 02:55:56
17 USA 2019-09-10 02:58:17
18 USA 2019-09-10 03:01:33
19 MX 2019-09-10 03:02:09
20 USA 2019-09-10 03:05:42
21 USA 2019-09-10 03:08:00
22 MX 2019-09-10 03:09:42
23 MX 2019-09-10 03:12:39"""
df2= pd.read_fwf(io.StringIO(raw), widths=[3, 8, 20])
df2['time']= df2['time'].astype('datetime64')