본문 바로가기
python

python datetime(today 조회, 날짜 계산, 연속 날짜 데이터 프레임 만들기)

by 괴로운데이빗 2025. 4. 7.

today 조회, 날짜 계산

import datetime

today_1 = datetime.datetime.today()
print( today_1 )
# 2025-04-07 11:26:42.904529

today_2 = datetime.date.today()
print( today_2 )
# 2025-04-07

today_3 = pd.to_datetime(today_2)
print( today_3 )
# 2025-04-07 00:00:00

yester_day_1 = today_3 + pd.to_timedelta(-1, unit="D")
print( yester_day_1 )
# 2025-04-06 00:00:00

 

연속 날짜 데이터 프레임 만들기

import pandas as pd

temp_df_1 = pd.DataFrame([1,2,3], index=["2025-04-01", "2025-04-02", "2025-04-03"], columns=["A"])
temp_df_1.index = pd.to_datetime(temp_df_1.index)
print(temp_df_1)
#                  A
# 2025-04-01  1
# 2025-04-02  2
# 2025-04-03  3
temp_df_2 = pd.DataFrame(temp_df_1, index=pd.date_range(temp_df_1.index.min(), "2025-04-05", freq="D"))
print(temp_df_2)
#                   A
# 2025-04-01  1.0
# 2025-04-02  2.0
# 2025-04-03  3.0
# 2025-04-04  NaN
# 2025-04-05  NaN

'python' 카테고리의 다른 글

np.squeeze, np.expand_dims(unsqueeze) 이해  (0) 2025.03.30
np.repeat 이해  (0) 2025.03.30
np.stack 이해  (0) 2025.03.30
VS code - 명령어 부분 실행  (0) 2025.03.23
__name__  (0) 2025.03.02
jupyter lab - git 롤백 기능  (0) 2025.02.09