본문 바로가기

Programing Language/Python

[datetime] datetime to yearly float

반응형

Introduction

datetime자체는 너무 좋지만, 장기 시계열의 경우, yearly float(decimal year)형이 필요한 경우 있다. 이를 컨버팅하는건 너무 쉽다!
정확히는 pd.datetime형태를 바꿔주는 것! 불러온 데이터 프레임은 1965-08-08 10:00:00와 같은 형태로 date가 들어있다. 이를 PyAstronomy를 이용해 1965.601142와 같이 바꿔줄 수 있다.

example code

from PyAstronomy import pyasl

df = pd.read_csv('./Data_hour_sorted/%s.csv'%station, index_col=None)
t = pd.to_datetime(df.date).values
date2 = [pyasl.decimalYear(pd.to_datetime(x)) for x in t]
df['date2'] = date2
반응형