파이썬/데이터분석

datetime index를 통해 pandas DataFrame 생성

배우고 성장하는 청년 블로그 2023. 2. 13. 15:21

# datetime을 이용하여 pandas index 생성

예를 들어, 2022-01-01 ~ 2022-01-31 데이터를 생성한다고 가정

 

1
2
3
4
5
6
7
8
9
10
# 2022-01-01 ~ 2022-01-31
start_date = pd.to_datetime('2022-01-01')
end_date = pd.to_datetime('2022-02-01')
 
# start_date ~ end_date
# freq = 1 hour
# left inclusive, right exclusive
data_index = pd.date_range(start_date, end_date, freq='T', inclusive='left')
 
df = pd.DataFrame(index=data_index)
cs

 

1) pandas의 to_datetime 함수를 통해 시작과 끝 날짜의 datetime 형식의 변수 생성

https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html

 

pandas.to_datetime — pandas 1.5.3 documentation

Specify a date parse order if arg is str or is list-like. If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10. Warning dayfirst=True is not strict, but will prefer to parse with day first. If a delimited date string cannot be

pandas.pydata.org

 

2) pandas의 date_range 함수를 통해 pandas datetime 형식의 index 생성

 - start_date, end_date로 시작과 끝 날짜 입력

 - freq 으로 주기를 입력함.

  - T: 1 hour, M: 1 month 등..

https://pandas.pydata.org/docs/reference/api/pandas.date_range.html

 

pandas.date_range — pandas 1.5.3 documentation

Make the interval closed with respect to the given frequency to the ‘left’, ‘right’, or both sides (None, the default). Deprecated since version 1.4.0: Argument closed has been deprecated to standardize boundary inputs. Use inclusive instead, to se

pandas.pydata.org

 

3) pandas의 DataFrame 함수를 통해 DataFrame 생성

 - 2번에서 생성한 index를 제공

 - 이를 통해, 인덱스는 제공했지만 내용이 빈 DataFrame이 생성됨.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html

 

pandas.DataFrame — pandas 1.5.3 documentation

Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform column selection instead.

pandas.pydata.org