728x90
파이썬에서는 데이터 분석 및 시계열 데이터 처리를 위해 pandas 라이브러리를 사용함
pandas의 Timestamp 클래스는 날짜와 시간 데이터를 효과적으로 다룰 수 있도록 해줌
날짜, 시간 생성
import pandas as pd
현재_시간 = pd.Timestamp.now()
print("현재 시간:", 현재_시간)
특정_시간 = pd.Timestamp(2024, 4, 12, 10, 30, 0)
print("특정 시간:", 특정_시간)
날짜 및 시간 형식화
import pandas as pd
현재_시간 = pd.Timestamp.now()
형식화된_시간 = 현재_시간.strftime('%Y-%m-%d %H:%M:%S')
print("형식화된 시간:", 형식화된_시간)
%Y, %m, %d, %H, %M, %S는 각각 연도, 월, 일, 시, 분, 초를 나타내는 포맷 문자열
timedelta를 사용한 날짜 및 시간 연산
import pandas as pd
현재_시간 = pd.Timestamp.now()
시간_차이 = pd.Timedelta(days=3, hours=5, minutes=30)
미래_시간 = 현재_시간 + 시간_차이
과거_시간 = 현재_시간 - 시간_차이
print("미래 시간:", 미래_시간)
print("과거 시간:", 과거_시간)
Pandas를 이용한 시계열 데이터 처리 예시
import pandas as pd
# 데이터 로드
데이터 = pd.read_csv('./cansim.csv')
# 날짜 및 시간 열 처리
데이터['adjustments'] = pd.to_datetime(데이터['adjustments'])
# 데이터 필터링
특정_기간 = (데이터['adjustments'] > '1991-10-31') & (데이터['adjustments'] <= '1992-02-29')
필터링된_데이터 = 데이터[특정_기간]
print(필터링된_데이터)
# 시계열 데이터 시각화
import matplotlib.pyplot as plt
plt.plot(데이터['adjustments'], 데이터['seasonally_adjusted'])
plt.xlabel('adjustments')
plt.ylabel('seasonally adjusted')
plt.title('Time Series Data Visualization')
plt.show()
더보기

실행결과
adjustments unadjusted seasonally_adjusted
10 1991-11-30 16237366 15662790
11 1991-12-31 18381340 15349625
12 1992-01-31 13084963 15477875
13 1992-02-29 12773972 15513022

728x90
'Python' 카테고리의 다른 글
Python 추상클래스 (0) | 2024.04.12 |
---|---|
Python Singleton 패턴 (0) | 2024.04.12 |
Python 날짜 시간 다루기 (feat. datetime) (0) | 2024.04.12 |
[작성중] Python 문서화 (feat. sphinx) (0) | 2024.04.12 |
Python 문서화 (feat. PEP 257) (0) | 2024.04.11 |