Select 数据基于工作日和周末 pandas

Select data based on weekday and weekend pandas

我有一个这样的 df:

import pandas as pd
import numpy as np

datetime = [('2019-09-15 00:15:00.000000000'),
            ('2019-09-15 00:30:00.000000000'),
            ('2019-09-15 00:45:00.000000000'),
            ('2019-09-15 01:00:00.000000000'),
            ('2019-09-15 01:15:00.000000000'),
            ('2019-09-15 01:30:00.000000000'),
            ('2019-09-15 01:45:00.000000000'),
            ('2019-09-15 02:00:00.000000000'),
            ('2019-09-15 02:15:00.000000000')]
p =[494.76,486.36,484.68,500.64,482.16,483.84,483.0,478.8,493.08,474.6]
q = [47.88,33.6,41.16,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

df = pd.DataFrame(zip(datetime, p, q), columns=['datetime','p','q'])
df['week'] (df['datetime'].astype('datetime64[ns]') + pd.Timedelta(seconds=-15*60)).dt.day_name()

现在我已经定义了星期几,其中每一天从 00:15:00 开始到 96 点后的 00:00:00 结束。我想将它们分成另外两个 df。一个只有工作日,另一个只有周末。我试过:

df_week = df[~df['datetime'].dt.day_name().isin(['Saturday','Sunday'])]
df_weekend = df[df['datetime'].dt.day_name().isin(['Saturday','Sunday'])] 

虽然代码运行没有错误,但我的输出包含 df_week 处“星期六”的一个数据。像这样:

知道如何解决这个问题吗?

我是 new-ish 到 Pandas,但我认为以下内容有效:

df1 = df[df['week'].isin(['Sunday', 'Saturday'])]

df2 = df[~df['week'].isin(['Sunday', 'Saturday'])]

希望有能力的人也帮忙验证一下

为了解决这个问题,我使用 .loc 函数:

monday = df.loc[(df['week'] == 'Monday')]
tuesday = df.loc[(df['week'] == 'Tuesday')]
df_w = pd.concat([monday,tuesday])

不是最快的解决方案,但它有效