Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何按月份和年份输入筛选具有日期时间索引的数据帧?熊猫_Python_Pandas_Datetime_Indexing - Fatal编程技术网

Python 如何按月份和年份输入筛选具有日期时间索引的数据帧?熊猫

Python 如何按月份和年份输入筛选具有日期时间索引的数据帧?熊猫,python,pandas,datetime,indexing,Python,Pandas,Datetime,Indexing,给定如下df: df=pd.read_csv(PATH + 'Matriz3_fechas.csv',index_col='Fecha',skiprows=0) df.index = pd.DatetimeIndex(df.index) 注意,Fecha已经是datetime格式的索引** Fecha D576972dc305aa D576972dc32e9a D576972dc3590a

给定如下
df

df=pd.read_csv(PATH + 'Matriz3_fechas.csv',index_col='Fecha',skiprows=0)
df.index = pd.DatetimeIndex(df.index)
注意,Fecha已经是datetime格式的索引**

 Fecha                D576972dc305aa  D576972dc32e9a  D576972dc3590a  
                                                             
2016-06-01 00:00:00         0.0          0.0               0.1  
2016-07-01 00:05:00         0.0          0.0               0.1  
2017-05-01 00:10:00         0.0          0.0               0.1  
2017-05-01 00:15:00         0.0          0.0               0.1                                                              
2017-07-01 00:20:00         0.0          0.0               0.1  
                                                                 
我尝试按月份和年份进行筛选:

df=df[(df.index.month==5)&(matriz.index.year==2017)]
但它不会过滤
df
以获得:(期望的结果)

您可以使用:

但您的解决方案也有效(如果
matriz
df
,我认为是打字错误):


打印(df.index)return
DatetimeIndex
?月与日不交换?我以前试过,但输出的形状是[0行x 3列],而不是[2行x 3列]@jezraelhmm,这意味着2017和
5
月没有数据。但是可能
2017-05-10
被替换为
2017-10-05
,因此它过滤错误。你能检查一下吗?@jezreal发现了问题,我的.csv没有更新,因此没有2017-05年的数据。然而你的回答帮助了我。
 Fecha                D576972dc305aa  D576972dc32e9a  D576972dc3590a  \
                                                             
2017-05-01 00:10:00         0.0          0.0               0.1  \
2017-05-01 00:15:00         0.0          0.0               0.1  \
#for datetimeindex use parameter parse_dates 
df=pd.read_csv(PATH+'Matriz3_fechas.csv',index_col='Fecha',skiprows=0,parse_dates=['Fecha'])

print (df.index)
DatetimeIndex(['2016-06-01 00:00:00', '2016-07-01 00:05:00',
               '2017-05-01 00:10:00', '2017-05-01 00:15:00',
               '2017-07-01 00:20:00'],
              dtype='datetime64[ns]', name='Fecha', freq=None)


df = df.loc['2017-05']
print (df)
                     D576972dc305aa  D576972dc32e9a  D576972dc3590a
Fecha                                                              
2017-05-01 00:10:00             0.0             0.0             0.1
2017-05-01 00:15:00             0.0             0.0             0.1
df=df[(df.index.month==5)&(df.index.year==2017)]
print (df)
                     D576972dc305aa  D576972dc32e9a  D576972dc3590a
Fecha                                                              
2017-05-01 00:10:00             0.0             0.0             0.1
2017-05-01 00:15:00             0.0             0.0             0.1