-
[Python] pandas datetime 타입 시간/주/일 더하기Python/Python For Analytics 2019. 9. 6. 13:38
시스템 로그를 분석할 때, 로그시간에 UTC시간을 더해줘야 할 때가 있는데,
datetime의 timedelta의 메소드를 이용하여 변환할 수 있겠다.
기준일
from datetime import datetime, timedelta ....... print(data['time']) 0 2019-08-27 00:00:00 1 2019-08-27 00:00:00 2 2019-08-27 00:00:00 3 2019-08-27 00:00:00 4 2019-08-27 00:00:00 ... 373064 2019-08-27 23:59:58 373065 2019-08-27 23:59:59 373066 2019-08-27 23:59:59 373067 2019-08-27 23:59:59 373068 2019-08-27 23:59:59 Name: time, Length: 373069, dtype: datetime64[ns]
9시간(hour) 더하기(UTC 한국시간)
data['time'] = pd.DatetimeIndex(data['time']) + timedelta(hours=9) print(data['time']) 0 2019-08-27 09:00:00 1 2019-08-27 09:00:00 2 2019-08-27 09:00:00 3 2019-08-27 09:00:00 4 2019-08-27 09:00:00 ... 373064 2019-08-28 08:59:58 373065 2019-08-28 08:59:59 373066 2019-08-28 08:59:59 373067 2019-08-28 08:59:59 373068 2019-08-28 08:59:59 Name: time, Length: 373069, dtype: datetime64[ns]
2일(day) 더하기
data['time'] = pd.DatetimeIndex(data['time']) + timedelta(weeks=1) print(data['time']) 0 2019-09-03 00:00:00 1 2019-09-03 00:00:00 2 2019-09-03 00:00:00 3 2019-09-03 00:00:00 4 2019-09-03 00:00:00 ... 373064 2019-09-03 23:59:58 373065 2019-09-03 23:59:59 373066 2019-09-03 23:59:59 373067 2019-09-03 23:59:59 373068 2019-09-03 23:59:59 Name: time, Length: 373069, dtype: datetime64[ns]
1주(week) 더하기
data['time'] = pd.DatetimeIndex(data['time']) + timedelta(weeks=1) print(data['time']) 0 2019-09-03 00:00:00 1 2019-09-03 00:00:00 2 2019-09-03 00:00:00 3 2019-09-03 00:00:00 4 2019-09-03 00:00:00 ... 373064 2019-09-03 23:59:58 373065 2019-09-03 23:59:59 373066 2019-09-03 23:59:59 373067 2019-09-03 23:59:59 373068 2019-09-03 23:59:59 Name: time, Length: 373069, dtype: datetime64[ns]
'Python > Python For Analytics' 카테고리의 다른 글
[Python] pandas groupby - count, max, min, mean, sum, agg (0) 2019.10.28 [Python] pandas의 sort_values를 이용한 dataframe 정렬 (0) 2019.10.24 [Python] Pandas를 이용한 IIS 웹 로그 분석 (sc-bytes, cs-bytes) (0) 2019.10.23 [Python] Pandas DataFrame 컬럼명 특정 문자로 변경 (0) 2019.09.25 [Python] list data type pandas의 DataFrame 만들기 (0) 2019.09.04 [Python] pandas를 이용한 IIS log 파싱 (0) 2019.08.30 [Python] pandas를 이용한 mariadb 결과값 다른 mariadb 테이블로 저장 (0) 2019.08.29 [Python] sqlite3 DB output to pandas dataframe. (sqlite3 db 퀄리결과 pandas dataframe 만들기) (0) 2019.08.12