1

I have seen many ways to locate a data frame for one date range,

i.e.

mask = (df["TimeStamp"] > date_range[0]) & (df["TimeStamp"] < date_range[1])
df = df.loc[mask]

but I can't find out how to do this if I had multiple date ranges

i.e.

date_ranges = [date_range_1, date_range_2, date_range_3, ... , date_range_n]

I would need something like

mask = ()

for date_range in date_ranges:
    sub_mask = (df["TimeStamp"] > date_range[0]) & (df["TimeStamp"] < date_range[1])
    mask.append(sub_mask)

df = df.loc[mask]

but of course this doesn't work for a variety of reasons (you need an or statement between the ands, and you cant append these masks in this way)

could anybody give me a nudge in the right direction?

4

2 回答 2

2

您可以使用以下方法更改代码:

mask = 0
for date_range in date_ranges:
    sub_mask = (df["TimeStamp"] > date_range[0]) & (df["TimeStamp"] < date_range[1])
    mask = (mask | sub_mask)
df = df.loc[mask]
于 2019-07-24T09:53:47.850 回答
0

由于 mask = (1 | mask) 将始终在最后一位返回 1

mask = 0
于 2019-07-24T09:58:24.163 回答