我有一个看起来像这样的numpy数组:
>>> array_data
array([[datetime.datetime(2017, 10, 24, 1, 3, 45, 104000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 3, 47, 901000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 3, 56, 214000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 4, 8, 11000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 4, 13, 120000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 4, 15, 714000), 50, 4],
[datetime.datetime(2017, 10, 24, 1, 4, 16, 214000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 4, 27, 323000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 5, 13, 261000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 5, 56, 276000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 6, 0, 886000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 6, 38, 104000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 6, 38, 995000), 50, -1],
[datetime.datetime(2017, 10, 24, 1, 6, 42, 511000), 51, 5],
[datetime.datetime(2017, 10, 24, 1, 7, 4, 714000), 50, 5],
[datetime.datetime(2017, 10, 24, 1, 7, 12, 823000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 7, 17, 229000), 50, -1],
[datetime.datetime(2017, 10, 24, 1, 7, 45, 948000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 7, 56, 245000), 50, 1],
[datetime.datetime(2017, 10, 24, 1, 8, 10, 761000), 50, -1],
[datetime.datetime(2017, 10, 24, 1, 8, 21, 464000), 50, -3],
[datetime.datetime(2017, 10, 24, 1, 8, 21, 761000), 50, -1]], dtype=object)
如果它是实时更新的,我怎样才能在更新时选择最新的数据?例如,如果当前时间为2017, 10, 24, 1, 7, 17, 229000
,我希望它打印出(50 * 5)+(50 * 1)+(50 * -1),如果是2017, 10, 24, 1, 7, 45, 948000
,它将打印输出(50 * 5)+(50 * 1)+(50 * -1)+(50 * 1)。
我认为我可以在更新时提取最新更新行的分钟值,然后进行向后循环并将它们的分钟值相互比较,直到它们不相同为止。但是,我认为当一分钟内有很多行并且更新比循环过程更快时,它将耗费资源。有没有更有效的方法来做到这一点?
答案 0 :(得分:1)
使用
从numpy数组创建数据框df = pd.DataFrame(array_data[:, 1:],
index=array_data[:, 0], columns=['a', 'b'])
或crate new并使用
添加行df = pd.DataFrame(columns=['a', 'b'])
df.loc[datetime.datetime.now()] = [0, 1]
然后你可以创建一个没有秒的 datetime 并用它来切片
>>> d
datetime.datetime(2017, 10, 24, 1, 8, 21, 761000)
>>> dm = datetime.datetime(d.year, d.month, d.day, d.hour, d.minute)
>>> dm
datetime.datetime(2017, 10, 24, 1, 8)
>>> df[dm:]
a b
2017-10-24 01:08:10.761 50 -1
2017-10-24 01:08:21.464 50 -3
2017-10-24 01:08:21.761 50 -1
您正在使用索引,因此它很有效。