如何用Deedle Frame< DateTime,_>中的某个键获取行的位置? [英] How to get the position of the row with some key from a Deedle Frame<DateTime,_>?

查看:53
本文介绍了如何用Deedle Frame< DateTime,_>中的某个键获取行的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按位置,我的意思是:

let position:int = positionForKey frame key
let row =
  Frame.take positionForKey
  |> frame.takeLast 1

然后, row 应该是只有一行的Frame,其键为 key .

Then, row should be a Frame with only one row, whose key is key.

我不知道如何实现 positionForKey .一个可行的想法,但我不知道这是否是最好的方法,那就是通过 Series.scanValues 创建另一个 Series 并将值作为位置,但我认为应该有一种更优雅的方法.

What I don't know is how to achieve positionForKey. One idea that should work but I don't know if it's the best way of doing it would be to create another Series via Series.scanValues and let the values be the positions, but I think there oughts to be a more elegant way of doing it.

通过 Series.scanValues 的实现为:

let positionForKey (frame:Frame<'K,_>) (key:'K) =
  let positions = Series.scanValues (fun pos _ -> pos + 1) 0 (frame.GetColumnAt 0)
  positions.[key]

...从 1

示例

假设您有一个这样的框架 f :

Say you have a Frame f like this:

03/01/01,  4 , ...
04/01/01,  3 , ...
05/01/01,  6 , ...
   ...  , ..., ...

然后, positionforKey f 04/01/01 = 2 positionforKey f 05/01/01 = 3 ,依此类推.(假设04/01/01是有效的DateTime)

then, positionforKey f 04/01/01 = 2, positionforKey f 05/01/01 = 3 and so on. (Supposing that 04/01/01 was a valid DateTime)

推荐答案

Deedle实际上具有用于执行此操作的内置功能,但是它们没有得到很好的记录(主要是因为当我们添加支持时,它已经发生了相当大的变化)虚拟框架").

Deedle actually has built-in function for doing this, but they are not very well documented (mostly because this has been changing quite a bit when we were adding support for "virtual frames").

但是,请考虑一个示例数据框:

However, consider a sample data frame:

let ts = series [ for i in 0 .. 365 -> DateTime(2017, 1, 1).AddDays(float i) => float i]
let df = frame ["Sample" => ts ]

数据帧具有一个行索引,该行索引表示如何使用索引执行查找.使用 RowIndex ,您可以找到键,然后将返回的地址转换为索引:

The data frame has a row index which represents how the lookup using indices is performed. Using the RowIndex, you can locate the key and then translate the returned address to an index:

let addr = df.RowIndex.Locate(DateTime(2017, 5, 1))
let idx = df.RowIndex.AddressOperations.OffsetOf(addr)

然后您可以得到只有这一行的框架:

And then you can get a frame with just this row:

df.GetRowsAt([| int idx |])

当使用内存数据帧时,地址 addr 只是索引,但是

The address addr is just the index when you are working with in-memory data frames, but in virtual data frames it would be a number that encodes where the row is stored and so it would not directly map to an offset. That's why I added the OffsetOf call, which maps the address to an actual index. Though in case of in-memory frames, you do not need to worry about this.

如果未找到密钥,则 addr 的值将为 -1L (尽管原则上,您应使用 Addressing.Address.invalid >进行检查时.)

If the key is not found, the addr value will be -1L (though in principle, you should use Addressing.Address.invalid when checking for this).

这篇关于如何用Deedle Frame&lt; DateTime,_&gt;中的某个键获取行的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆