2016-02-10 59 views

回答

0

IIUC,从样本数据帧起始为:

   Date x 
0 2016-01-01 20:01 1 
1 2016-01-02 20:02 2 

,你可以这样做:

     Date x 
2016-01-01 2016-01-01 20:01 1 
2016-01-02 2016-01-02 20:02 2 
+0

Thans Fabio,这似乎工作。考虑到许多研究领域可能在同一日期有很多数据读数,是否可以将另一个字段设置为索引以及日期,并且可以同时访问它们?例如说温度 - 电台,然后以日期为指标。或股票:股票名称,然后日期? – yoshiserry

+0

很高兴帮助。但请考虑发布另一个问题来寻求其他答案。最后,如果我的答案适合您的目的,请考虑接受它以帮助其他用户解决同样的问题。 –

0

使用​​:

df = df.set_index(pd.DatetimeIndex(df['Date']).date) 

只与date部分返回您DatetimeIndex将列转换为索引。你不需要修剪它的工作时间部分。

df = pd.DataFrame({ 
    'ts': pd.date_range('2015-12-20', periods=10, freq='12h'), 
    'stuff': np.random.randn(10) 
    }) 
print(df) 

     stuff     ts 
0 0.942231 2015-12-20 00:00:00 
1 1.229604 2015-12-20 12:00:00 
2 -0.162319 2015-12-21 00:00:00 
3 -0.142590 2015-12-21 12:00:00 
4 1.057184 2015-12-22 00:00:00 
5 -0.370927 2015-12-22 12:00:00 
6 -0.358605 2015-12-23 00:00:00 
7 -0.561857 2015-12-23 12:00:00 
8 -0.020714 2015-12-24 00:00:00 
9 0.552764 2015-12-24 12:00:00 

print(df.set_index('ts').ix['2015-12-21':'2015-12-23']) 

         stuff 
ts       
2015-12-21 00:00:00 -0.162319 
2015-12-21 12:00:00 -0.142590 
2015-12-22 00:00:00 1.057184 
2015-12-22 12:00:00 -0.370927 
2015-12-23 00:00:00 -0.358605 
2015-12-23 12:00:00 -0.561857