Python 时间序列数据的条件语句

Python 时间序列数据的条件语句,python,pandas,dataframe,time-series,conditional-statements,Python,Pandas,Dataframe,Time Series,Conditional Statements,我试图对时间序列数据执行条件语句。如果时间介于1)00:00:00和02:00:00 2)04:00:00和06:00:00之间,是否有办法将“t_值”设置为零 t_value 2019-11-24 00:00:00 4.0 2019-11-24 01:00:00 7.8 2019-11-24 02:00:00 95.1 2019-11-24 03:00:00 78.4 2019-11-24 04:00:00 8.0 2019-11-24 05:0

我试图对时间序列数据执行条件语句。如果时间介于1)00:00:00和02:00:00 2)04:00:00和06:00:00之间,是否有办法将“t_值”设置为零

                   t_value
2019-11-24 00:00:00  4.0
2019-11-24 01:00:00  7.8
2019-11-24 02:00:00  95.1
2019-11-24 03:00:00  78.4
2019-11-24 04:00:00  8.0
2019-11-24 05:00:00  17.50
2019-11-24 06:00:00  55.00
2019-11-24 07:00:00  66.00
2019-11-25 00:00:00  21.00
2019-11-25 01:00:00  12.40 

if-else
&
np。其中
是可能的选项,但我不确定如何在小时数上实施条件。

您可以使用
time
从日期时间索引中获取时间,并根据您的条件创建掩码。然后使用
loc
|
将掩码连接为或

#sample data
df = pd.DataFrame({'t_value':range(1,11)}, 
              index=pd.date_range('2020-05-17 00:00:00', periods=10, freq='1H'))

# masks
m1 = ((df.index.time>=pd.to_datetime('00:00:00').time()) 
      & (df.index.time<=pd.to_datetime('02:00:00').time()))
m2 = ((df.index.time>=pd.to_datetime('04:00:00').time())
      & (df.index.time<=pd.to_datetime('06:00:00').time()))

#set the value to 0
df.loc[m1|m2, 't_value'] = 0

print (df)
                     t_value
2020-05-17 00:00:00        0
2020-05-17 01:00:00        0
2020-05-17 02:00:00        0
2020-05-17 03:00:00        4
2020-05-17 04:00:00        0
2020-05-17 05:00:00        0
2020-05-17 06:00:00        0
2020-05-17 07:00:00        8
2020-05-17 08:00:00        9
2020-05-17 09:00:00       10
#示例数据
df=pd.DataFrame({'t_值]:范围(1,11)},
指数=pd.日期\范围('2020-05-17 00:00:00',周期=10,频率=1H'))
#面具
m1=((df.index.time>=pd.to_datetime('00:00:00').time())
&(df.index.time=pd.to_datetime('04:00:00').time())

&(df.index.time您可以使用
time
从日期时间索引访问时间,并根据您的情况创建掩码。然后使用
loc
|
将掩码连接为或

#sample data
df = pd.DataFrame({'t_value':range(1,11)}, 
              index=pd.date_range('2020-05-17 00:00:00', periods=10, freq='1H'))

# masks
m1 = ((df.index.time>=pd.to_datetime('00:00:00').time()) 
      & (df.index.time<=pd.to_datetime('02:00:00').time()))
m2 = ((df.index.time>=pd.to_datetime('04:00:00').time())
      & (df.index.time<=pd.to_datetime('06:00:00').time()))

#set the value to 0
df.loc[m1|m2, 't_value'] = 0

print (df)
                     t_value
2020-05-17 00:00:00        0
2020-05-17 01:00:00        0
2020-05-17 02:00:00        0
2020-05-17 03:00:00        4
2020-05-17 04:00:00        0
2020-05-17 05:00:00        0
2020-05-17 06:00:00        0
2020-05-17 07:00:00        8
2020-05-17 08:00:00        9
2020-05-17 09:00:00       10
#示例数据
df=pd.DataFrame({'t_值]:范围(1,11)},
指数=pd.日期\范围('2020-05-17 00:00:00',周期=10,频率=1H'))
#面具
m1=((df.index.time>=pd.to_datetime('00:00:00').time())
&(df.index.time=pd.to_datetime('04:00:00').time())
&(df.index.time使用获取指定时间之间的日期时间,然后使用分配新值:

我将使用@Ben.T的示例数据:

df = pd.DataFrame({'t_value':range(1,11)}, 
              index=pd.date_range('2020-05-17 00:00:00', periods=10, freq='1H'))

#get the time indices for the different ranges
m1 = df.between_time('00:00:00','02:00:00').index
m2 = df.between_time('04:00:00','06:00:00').index

#assign 0 to the t_value column matches : 
df.loc[m1|m2] = 0

print(df)

            t_value
2020-05-17 00:00:00 0
2020-05-17 01:00:00 0
2020-05-17 02:00:00 0
2020-05-17 03:00:00 4
2020-05-17 04:00:00 0
2020-05-17 05:00:00 0
2020-05-17 06:00:00 0
2020-05-17 07:00:00 8
2020-05-17 08:00:00 9
2020-05-17 09:00:00 10
使用获取指定时间之间的日期时间,然后使用分配新值:

我将使用@Ben.T的示例数据:

df = pd.DataFrame({'t_value':range(1,11)}, 
              index=pd.date_range('2020-05-17 00:00:00', periods=10, freq='1H'))

#get the time indices for the different ranges
m1 = df.between_time('00:00:00','02:00:00').index
m2 = df.between_time('04:00:00','06:00:00').index

#assign 0 to the t_value column matches : 
df.loc[m1|m2] = 0

print(df)

            t_value
2020-05-17 00:00:00 0
2020-05-17 01:00:00 0
2020-05-17 02:00:00 0
2020-05-17 03:00:00 4
2020-05-17 04:00:00 0
2020-05-17 05:00:00 0
2020-05-17 06:00:00 0
2020-05-17 07:00:00 8
2020-05-17 08:00:00 9
2020-05-17 09:00:00 10

因此,在您的示例中,只有7am会有值?是的,但这只是一个示例。我会将其编辑为更合适的示例。因此,在您的示例中,只有7am会有值?是的,但这只是一个示例。我会将其编辑为更合适的示例。如果我想使用时间范围(00:00,03:00:00)和t_值>0来计算一个新列,该列将它们分类为正确(>0)和不正确(=0)?在我的脑后,听起来像是你可以放入
loc
,带有
m1 | m2
部分的东西。你可以编辑你的问题以添加内容,尽管最好是问一个新问题,或者发布一个新问题,如果你想尝试一下,如果我想使用时间范围(00:00,03:00:00)和t_值>0来计算一个新列,该列将它们分类为正确(>0)和不正确(=0)?从我的脑后,听起来像是你可以在
loc
中加入
m1 | m2
部分的东西。你可以编辑你的问题以添加内容,尽管如果你想尝试的话,最好是问一个新问题,或者是发布一个新问题