简体   繁体   English

如何在Pandas中使用datetime index属性选择df的观测值?

[英]How to select observations of df using datetime index atributes in Pandas?

Given a df of this kind, where we have DateTime Index: 鉴于这种类型的df,我们有DateTime Index:

DateTime               A                           
2007-08-07 18:00:00    1
2007-08-08 00:00:00    2
2007-08-08 06:00:00    3
2007-08-08 12:00:00    4
2007-08-08 18:00:00    5
2007-11-02 18:00:00    6
2007-11-03 00:00:00    7
2007-11-03 06:00:00    8
2007-11-03 12:00:00    9
2007-11-03 18:00:00   10

I would like to subset observations using the attributes of the index, like: 我想使用索引的属性来进行子集观察,例如:

  • First business day of the month 本月的第一个工作日
  • Last business day of the month 本月最后一个工作日
  • First Friday of the month 'WOM-1FRI' 本月第一个星期五'WOM-1FRI'
  • Third Friday of the month 'WOM-3FRI' 本月第三个星期五'WOM-3FRI'

I'm specifically interested to know if this can be done using something like: 我特别想知道是否可以使用以下内容完成:

df.loc[(df['A'] < 5) & (df.index == 'WOM-3FRI'), 'Signal'] = 1

Thanks 谢谢

You could try... 你可以试试......

# FIRST DAY OF MONTH
df.loc[df[1:][df.index.month[:-1]!=df.index.month[1:]].index]

# LAST DAY OF MONTH
df.loc[df[:-1][df.index.month[:-1]!=df.index.month[1:]].index]

# 1st Friday
fr1 = df.groupby(df.index.year*100+df.index.month).apply(lambda x: x[(x.index.week==1)*(x.index.weekday==4)])

# 3rd Friday
fr3 = df.groupby(df.index.year*100+df.index.month).apply(lambda x: x[(x.index.week==3)*(x.index.weekday==4)])

If you want to remove extra-levels in the index of fr1 and fr3 : 如果要删除fr1fr3索引中的额外级别:

fr1.index=fr1.index.droplevel(0)
fr3.index=fr3.index.droplevel(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM