2015-09-08 45 views
2

我有一个数据帧,看起来像这样:在大熊猫数据帧修改小时datetimeindex

master.head(5) 
Out[73]: 
      hour price 
day      
2014-01-01  0 1066.24 
2014-01-01  1 1032.11 
2014-01-01  2 1028.53 
2014-01-01  3 963.57 
2014-01-01  4 890.65 


In [74]: master.index.dtype 

Out[74]: dtype('<M8[ns]') 

我需要做的就是更新与该列中的小时指标小时,但下列方法不要” t工作:

In [82]: master.index.hour = master.index.hour(master['hour']) 

TypeError: 'numpy.ndarray' object is not callable 

In [83]: master.index.hour = [master.index.hour(master.iloc[i,0]) for i in len(master.index.hour)] 

TypeError: 'int' object is not iterable 

如何继续?

+0

你的预期结果是什么? – styvane

+0

对不起,如果我不清楚,EdChum建议给出预期结果 – marpis

回答

3

IIUC我想你想构建一个TimedeltaIndex

In [89]: 
df.index += pd.TimedeltaIndex(df['hour'], unit='h') 
df 

Out[89]: 
        hour price 
2014-01-01 00:00:00  0 1066.24 
2014-01-01 01:00:00  1 1032.11 
2014-01-01 02:00:00  2 1028.53 
2014-01-01 03:00:00  3 963.57 
2014-01-01 04:00:00  4 890.65 

只是比较反对使用apply

In [87]: 
%timeit df.index + pd.TimedeltaIndex(df['hour'], unit='h') 
%timeit df.index + df['hour'].apply(lambda x: pd.Timedelta(x, 'h')) 

1000 loops, best of 3: 291 µs per loop 
1000 loops, best of 3: 1.18 ms per loop 

你可以看到,使用TimedeltaIndex是显著快

+0

太棒了!我已经呆了一个小时,并没有设法做到这一点! – marpis

+1

构建一个Timedeltaindex,会更快,发布时间 – EdChum

+0

你甚至不需要明确地构造一个TimedeltaIndex,只是''df.index + pd.Timedelta('1h')'' – Jeff

0
master.index = 
pd.to_datetime(master.index.map(lambda x : x.strftime('%Y-%m-%d')) + '-' + master.hour.map(str) , format='%Y-%m-%d-%H.0') 
+0

不受支持的操作数类型为+:'时间戳'和'str' – marpis

+0

您可能需要将“value '到字符串,检查更新的答案 –

+0

有点上下文或解释总是不错... – DavidW