2017-12-21 4 views
0

을 이동 할 수 없습니다 :나는 월요일 자정를 시작하고 토요일 자정 끝나는 dataframe이 팬더 dataframe 지수 조건부

>>> dfn.head() 
Out[8]: 
2012-02-27 00:00:00+00:00 3054679365.000 
2012-02-27 01:00:00+00:00 1433475236.000 
2012-02-27 02:00:00+00:00 1725293108.000 
2012-02-27 03:00:00+00:00 1089842336.000 
2012-02-27 04:00:00+00:00 1637301178.000 

>>> dfn.tail() 
2012-03-02 20:00:00+00:00 3696373423.000 
2012-03-02 21:00:00+00:00 3423657296.000 
2012-03-02 22:00:00+00:00 1887346076.000 
2012-03-02 23:00:00+00:00  426382220.400 
2012-03-03 00:00:00+00:00  759307738.400 
dtype: float64 

주파수는 시간당하지만 1에서 시작 '2012-03-02'에 휴식이있다 대신 자정의 생각 :

>>> dfn['2012-03-01'].tail() 
Out[12]: 
2012-03-01 19:00:00+00:00 2144039255.000 
2012-03-01 20:00:00+00:00 4055718131.000 
2012-03-01 21:00:00+00:00 1850226718.000 
2012-03-01 22:00:00+00:00 738256967.900 
2012-03-01 23:00:00+00:00 1163600574.000 
Name: vol, dtype: float64 

>>> dfn['2012-03-02'].head() 
Out[11]: 
2012-03-02 01:00:00+00:00 2364896887.000 
2012-03-02 02:00:00+00:00 1598799781.000 
2012-03-02 03:00:00+00:00 2011619242.000 
2012-03-02 04:00:00+00:00 2408284057.000 
2012-03-02 05:00:00+00:00 2084405746.000 
Name: vol, dtype: float64 

내가 '2012-03-02'1 오전 브레이크 포인트에서 시작 1 시간 인덱스를 이동하고 싶습니다.

trouble_spots = pd.date_range(start = dfn.index[trouble_loc], end = dfn.index[-1], freq='H', tz= 'Europe/London') 
>>> trouble_spots 
Out[13]: DatetimeIndex(['2012-03-02 01:00:00+00:00', '2012-03-02 02:00:00+00:00', '2012-03-02 03:00:00+00:00',.... '2012-03-02 22:00:00+00:00', '2012-03-02 23:00:00+00:00', '2012-03-03 00:00:00+00:00'], dtype='datetime64[ns]', freq='H', tz='Europe/London') 

문제는 작동하지 않는 다음과 같은 것입니다 :

dfn.index = dfn.index.map(lambda x: x - pd.Timedelta(1, 'h') if x in trouble_spots else x) 

그것은 이전과 같은 인덱스를 제공합니다 나는 다음 시도했다. 각 부분은 별도로 작동합니다 :

>>> [x for x in dfn.index if x in trouble_spots] 
Out[6]: 
[Timestamp('2012-03-02 01:00:00+0000', tz='Europe/London'), 
Timestamp('2012-03-02 02:00:00+0000', tz='Europe/London'), 
...... 
Timestamp('2012-03-02 03:00:00+0000', tz='Europe/London'), 
Timestamp('2012-03-02 21:00:00+0000', tz='Europe/London'), 
Timestamp('2012-03-02 22:00:00+0000', tz='Europe/London'), 

dfn.index.map(lambda x: x - pd.Timedelta(1, 'h')) 
Out[5]: 
DatetimeIndex(['2012-02-26 23:00:00+00:00', '2012-02-27 00:00:00+00:00', ... '2012-03-02 20:00:00+00:00', '2012-03-02 21:00:00+00:00', '2012-03-02 22:00:00+00:00', '2012-03-02 23:00:00+00:00'], dtype='datetime64[ns]', length=120, freq=None, tz='Europe/London') 

하지만 함께 사용하면 작동하지 않는 것 같습니다. 내가 여기서 누락 된 것이 있습니까?

답변

0

코드를 수정하는 방법을 잘 모르겠지만 귀하의 질문을 올바르게 이해할 수 있다면 "나는 색인이 있습니다. 색인에서 한 항목을 삭제하고 싶습니다."라고 생각하면됩니다. 그러나 또한 "나는 가치가있다. 나는 그것들을 삭제하고 싶지 않다." 그렇다면 왜 하나의 인덱스 값을 지우지 않고 값을 동일하게 유지하는 완전히 새로운 데이터 프레임을 만들지 만 제거한 누락 값 1 개를 계산할 때 새 인덱스 +1 시간을 추가하십시오.

예 :

import pandas as pd 
import numpy as np 
np.random.seed(1) 

index = pd.PeriodIndex(start='2012-03-02 00:00:00+00:00', freq='h', periods=48) 
values = np.random.randint(7382569, 40557181, len(index)) 
df = pd.DataFrame(data=values, index=index) 

new_index = df.index.delete(df.index.get_loc('2012-03-03 00:00:00+00:00')) 
index_plus_one = new_index.append(pd.Index([pd.Period(new_index.max() + pd.Timedelta('1h'))])) 
new_df = pd.DataFrame(data=values, index=index_plus_one) 
print('PREVIOUS DF') 
print(df.iloc[18: 30]) 

print('NEW DF') 
print(new_df.iloc[18: 30]) 

인쇄 :

PREVIOUS DF 
         0 
2012-03-02 18:00 39881435 
2012-03-02 19:00 18381629 
2012-03-02 20:00 29424423 
2012-03-02 21:00 10704782 
2012-03-02 22:00 31142331 
2012-03-02 23:00 12152829 
2012-03-03 00:00 13083060 
2012-03-03 01:00 32950053 
2012-03-03 02:00 20771859 
2012-03-03 03:00 20330693 
2012-03-03 04:00 24348102 
2012-03-03 05:00 20971447 
NEW DF 
         0 
2012-03-02 18:00 39881435 
2012-03-02 19:00 18381629 
2012-03-02 20:00 29424423 
2012-03-02 21:00 10704782 
2012-03-02 22:00 31142331 
2012-03-02 23:00 12152829 
2012-03-03 01:00 13083060 
2012-03-03 02:00 32950053 
2012-03-03 03:00 20771859 
2012-03-03 04:00 20330693 
2012-03-03 05:00 24348102 
2012-03-03 06:00 20971447 
관련 문제