英文:
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 -> (df.datetime .>= oh.start) .& (df.datetime .<= 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
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> filter(:moments => inrange(ranges), df)
2×1 DataFrame
Row │ moments
─────┼────────────────────
1 │ 0005-01-01T00:00:00
2 │ 0060-01-01T00:00:00
(如果有什么不清楚的,请留下评论;请注意,inrange
是一个高阶函数,返回一个接受时间点以检查的函数)
英文:
I would do this this way:
julia> ranges = [(start=DateTime(0),stop=DateTime(10)), (start=DateTime(50),stop=DateTime(70))]
2-element 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 (generic function with 1 method)
julia> df = DataFrame(moments = [DateTime(-1), DateTime(5), DateTime(20), DateTime(60), DateTime(80)])
5×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> filter(:moments => inrange(ranges), df)
2×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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论