2013-07-24 1 views
2

일련의 datetime64 [ns] 개체가 있습니다.Python, datetime64의 시리즈를 날짜와 분으로 변환

날짜 및 분 (HH : MM) 부분을 추출하고 싶습니다. 지금까지 아래 코드를 사용하고 있지만 매우 느립니다. 어떻게하면 더 효과적으로 할 수 있을까요?

>>> type(df['EXECUTION_TIMESTAMP']) 
Out[1]: pandas.core.series.Series 


>>> df['EXECUTION_TIMESTAMP'] 
Out[1]: 
0 2012-12-13 16:46:37 
1 2012-12-13 16:46:42 
2 2012-12-13 16:46:47 

... 

68 2010-09-07 15:21:38 
69 2013-07-21 21:40:14 
70 2010-07-21 22:44:46 
Name: EXECUTION_TIMESTAMP, Length: 769552, dtype: datetime64[ns] 


# Get the DateTimes Only 
ets = pd.Series(df['EXECUTION_TIMESTAMP']) 

print('Converting times') 
dt_min = [] 
dd  = [] 
for x in ets: 
    dt_min.append(pd.datetime(2000,1,1,x.hour,x.minute)) 
    dd.append(pd.datetime(x.year,x.month,x.day)) 
튜플 목록으로하지 표시

답변

2
In [1]: df = DataFrame(dict(time = Series([Timestamp('20121213 16:46:37'),Timestamp('20121213 16:46:42'),Timestamp('20121213 16:46:47'),Timestamp('20100907 16:21:38')]))) 

In [2]: df 
Out[2]: 
       time 
0 2012-12-13 16:46:37 
1 2012-12-13 16:46:42 
2 2012-12-13 16:46:47 
3 2010-09-07 16:21:38 

In [3]: df.dtypes 
Out[3]: 
time datetime64[ns] 
dtype: object 

In [4]: index = pd.DatetimeIndex(df['time']) 

In [5]: index 
Out[5]: 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2012-12-13 16:46:37, ..., 2010-09-07 16:21:38] 
Length: 4, Freq: None, Timezone: None 

In [6]: zip(index.minute,index.second) 
Out[6]: [(46, 37), (46, 42), (46, 47), (21, 38)] 

하지만, 시간과 날짜로, 분은 내가 당신을 생각하고

In [10]: Series([ datetime.datetime(2000,1,1,t.hour,t.minute) for t in pd.DatetimeIndex(df['time']).time ]) 
Out[10]: 
0 2000-01-01 16:46:00 
1 2000-01-01 16:46:00 
2 2000-01-01 16:46:00 
3 2000-01-01 16:21:00 
dtype: datetime64[ns] 

결국 이것에 의해 그룹화 할 추출; 그냥 직접하십시오

df.set_index('time').groupby(lambda x: x.hour,lambda x: x.minute).apply(...) 
+0

도움 주셔서 감사합니다. 마지막 zip 명령은 튜플 목록을 분 및 초로 지정합니다. 대신 datetime의 데이터 프레임을 반환하는 방법이 있습니까? – Ginger

+0

최종 목표는 무엇인지 확실하지 않지만 도움이되는지 알려주세요. – Jeff

관련 문제