julia DataFrame 在多个 DateTime 范围上选择

huangapple go评论96阅读模式
英文:

julia DataFrame select over multiple DateTime ranges

问题

如何从DataFrame中选择多个日期时间范围?

ranges = [(start=DateTime(0), stop=DateTime(10)), (start=DateTime(50), stop=DateTime(70))]

dt_ranges = map(oh -> (df.datetime >= oh.start) & (df.datetime <= oh.stop), ranges)

df[dt_ranges, j]

这不会选择这些日期时间范围。它会出现ArgError错误。

英文:

How does one select multiple datetime ranges from a DataFrame?

ranges = [(start=DateTime(0),stop=DateTime(10)), (start=DateTime(50),stop=DateTime(70))]

dt_ranges = map(oh -&gt; (df.datetime .&gt;= oh.start) .&amp; (df.datetime .&lt;= oh.stop), ranges)

df[dt_ranges, j]

This doesn't select the ranges. It errors out with ArgError.

答案1

得分: 2

我会这样做:

julia> ranges = [(start=DateTime(0),stop=DateTime(10)), (start=DateTime(50),stop=DateTime(70))]
2个元素的 Vector{NamedTuple{(:start, :stop), Tuple{DateTime, DateTime}}}:
 (start = DateTime("0000-01-01T00:00:00"), stop = DateTime("0010-01-01T00:00:00"))
 (start = DateTime("0050-01-01T00:00:00"), stop = DateTime("0070-01-01T00:00:00"))

julia> inrange(ranges) = moment -> any(x -> x.start <= moment <= x.stop, ranges)
inrange (拥有 1 个方法的通用函数)

julia> df = DataFrame(moments = [DateTime(-1), DateTime(5), DateTime(20), DateTime(60), DateTime(80)])
5×1 DataFrame
 Row │ moments           
─────┼────────────────────
   1-0001-01-01T00:00:00
   20005-01-01T00:00:00
   30020-01-01T00:00:00
   40060-01-01T00:00:00
   50080-01-01T00:00:00

julia> filter(:moments => inrange(ranges), df)
2×1 DataFrame
 Row │ moments           
─────┼────────────────────
   10005-01-01T00:00:00
   20060-01-01T00:00:00

(如果有什么不清楚的,请留下评论;请注意,inrange 是一个高阶函数,返回一个接受时间点以检查的函数)

英文:

I would do this this way:

julia&gt; ranges = [(start=DateTime(0),stop=DateTime(10)), (start=DateTime(50),stop=DateTime(70))]
2-element Vector{NamedTuple{(:start, :stop), Tuple{DateTime, DateTime}}}:
 (start = DateTime(&quot;0000-01-01T00:00:00&quot;), stop = DateTime(&quot;0010-01-01T00:00:00&quot;))
 (start = DateTime(&quot;0050-01-01T00:00:00&quot;), stop = DateTime(&quot;0070-01-01T00:00:00&quot;))

julia&gt; inrange(ranges) = moment -&gt; any(x -&gt; x.start &lt;= moment &lt;= x.stop, ranges)
inrange (generic function with 1 method)

julia&gt; df = DataFrame(moments = [DateTime(-1), DateTime(5), DateTime(20), DateTime(60), DateTime(80)])
5&#215;1 DataFrame
 Row │ moments
     │ DateTime
─────┼──────────────────────
   1 │ -0001-01-01T00:00:00
   2 │ 0005-01-01T00:00:00
   3 │ 0020-01-01T00:00:00
   4 │ 0060-01-01T00:00:00
   5 │ 0080-01-01T00:00:00

julia&gt; filter(:moments =&gt; inrange(ranges), df)
2&#215;1 DataFrame
 Row │ moments
     │ DateTime
─────┼─────────────────────
   1 │ 0005-01-01T00:00:00
   2 │ 0060-01-01T00:00:00

(if something is unclear please comment; note that inrange is a higher-order function returning a function that takes a time moment to check)

huangapple
  • 本文由 发表于 2023年7月14日 07:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定