2017-05-22 12 views
1

datetime64[ns] 형식의 시간 부분을 1900-01-01 00:32:59으로 어떻게 만듭니 며 총 초로 변환합니까?팬더에서 문자열 데이터 형식 지속 시간을 날짜 형식으로 변환

1900-01-01 00:00:00에서 정확히 (32 * 60 + 59) 초가되는이 예제에서 최종 결과는 초 단위로 1979입니다.

+0

[팬더]의 가능한 복제본입니다. MM : SS를 사용하여 열을 변환하고 초 단위로 십진수] (http://stackoverflow.com/questions/20472413/pandas-transform-column-with-mmss-decimals-into-number-of-seconds) – spies006

답변

0

당신은 to_timedelta + strftime + total_seconds + astype 사용할 수 있습니다

df = pd.DataFrame({"date": ["1900-01-01 00:32:59"]}) 
df['date'] = pd.to_datetime(df['date']) 
print (df) 
       date 
0 1900-01-01 00:32:59 

df['total'] = pd.to_timedelta(df['date'].dt.strftime("%H:%M:%S")) 
       .dt.total_seconds().astype(int) 
print (df) 
       date total 
0 1900-01-01 00:32:59 1979 

또는 floor + total_seconds + astype :

df['total'] = (df['date'] - df['date'].dt.floor('D')).dt.total_seconds().astype(int) 
print (df) 
       date total 
0 1900-01-01 00:32:59 1979 

또는 normalize + total_seconds + astype

df['total'] = (df['date'] - df['date'].dt.normalize()).dt.total_seconds().astype(int) 
print (df) 
       date total 
0 1900-01-01 00:32:59 1979