인공지능, 데이터분석/[Numpy, Pandas] EDA, 문법

[Pandas] datetime 모듈에 대해 알기 !

마법사 코딩공주 2023. 5. 25. 21:39
728x90
반응형

📌  datetime 모듈은 날짜와 시간 정보를 다루는데 사용

datetime 모듈을 import하면, datetime 객체를 사용할 수 있습니다.

datetime 객체는 현재 날짜와 시간 정보를 가져오는 now() 메서드와 특정 날짜와 시간 정보를 가지는 객체를 생성하는 등 다양한 메서드와 속성이 있습니다.

 

datetime: 날짜와 시간을 동시에 포함하는 객체를 생성하고 조작하는 클래스입니다.

  - year, month, day, hour, minute, second 등의 속성을 사용하여 날짜 및 시간 구성요소에 접근할 수 있습니다.

date : 날짜 정보만을 포함하는 객체를 생성하고 조작하는 클래스입니다. 

   - year, month, day 등의 속성을 사용하여 날짜 구성요소에 접근할 수 있습니다.

time : 시간 정보만을 포함하는 객체를 생성하고 조작하는 클래스입니다.

  - hour, minute, second, microsecond 등의 속성을 사용하여 시간 구성요소에 접근할 수 있습니다.

timedelta : 두 날짜 또는 시간 간의 차이를 표현하는 객체를 생성하고 조작하는 클래스입니다.

  -  일(day), 초(second), 마이크로초(microsecond) 등의 단위로 시간 간격을 표현할 수 있습니다.

●  datetime.now(): 현재 날짜와 시간을 반환합니다.

● datetime.strptime(): 문자열을 날짜와 시간 객체로 변환하는 함수입니다.

  - 지정된 형식에 맞게 문자열을 해석하여 datetime 객체로 반환합니다.

● datetime.strftime(): 날짜와 시간 객체를 지정된 형식의 문자열로 변환하는 함수입니다.

  - strftime() 메서드를 호출하여 원하는 형식의 문자열로 변환할 수 있습니다.


 

예를 들어, 현재 날짜와 시간 정보를 가져오는 코드는 다음과 같습니다.

from datetime import datetime
current_dateTime = datetime.now()
print(current_dateTime)

datetime 객체에서 날짜와 시간 정보를 추출하는 방법

from datetime import datetime
current_dateTime = datetime.now()
print(current_dateTime.year) # 2023
print(current_dateTime.month) # 5
print(current_dateTime.day) # 25
print(current_dateTime.hour) # 21
print(current_dateTime.minute) # 30
print(current_dateTime.second) # 42
print(current_dateTime.microsecond) # 735091
728x90
반응형