#!/usr/bin/env python # coding: utf-8 # # **Django ORM Datetime** # - 일반적 ORM 객체등은 원칙적 **datetime.dateime** 객체이다 # - **datetime.date** 객체로만 관리되면 문제가 없지만 # - **datetime.datetime** 에서 **date** 만 활용하는 경우에도 시간대가 다르면 날짜도 다르게 출력 # - **Pandas Datetime Index 객체** 는 추가적 편리한 메소드들을 제공 # # ## **1 tzinfo Convert : datetime** # In[1]: # Django ORM 데이터 tz_info 변경 및 확인 # Django DateTime => datetime.datetime 객체를 출력 import pytz seoul = pytz.timezone("Asia/Seoul") # time_check = PriceKRX.objects.all().last().datetime # time_check.replace(tzinfo=seoul) # ## **2 tzinfo Convert : pd.DataFrame** # In[ ]: # DataFrame 의 tz_info 변경 import pytz import pandas as pd seoul = pytz.timezone("Asia/Seoul") # df.datetime = list(map( # lambda x : seoul.localize(x), # df.datetime.tolist()) # ) # In[ ]: import pytz from krxstock.shinhan import get_invest seoul = pytz.timezone("Asia/Seoul") df = get_invest("005930") df["시간"][0].replace(tzinfo=seoul) # from krx.tasks import get_trading_invest # get_trading_invest() # In[ ]: from glob import glob from krx.tasks import get_trader_price # files = glob("../jupyter/data/trade/csv/*.*") # get_trader_price(files[0]) # # **Python Datetime** # ## **1 tzinfo Convert : datetime** # In[1]: import datetime timestamp = 1528797322 date_time = datetime.datetime.fromtimestamp(timestamp) print( date_time.strftime("%m/%d/%Y, %H:%M:%S") ) print( date_time.strftime("%d %b, %Y") ) print( date_time.strftime("%d %B, %Y") ) # In[4]: # datetime 모듈의 활용 import datetime print( datetime.datetime.today().strftime('%Y-%m-%d') ) print( datetime.datetime(year=2000, month=1, day=1) ) print( datetime.datetime.strptime('09/19/18 13:55:26', '%m/%d/%y %H:%M:%S') ) datetime.datetime.today() - datetime.timedelta(days=7) # In[1]: from datetime import datetime dateString = "7-May-2018" dateFormatter = "%u-%b-%Y" datetime.strptime(dateString, dateFormatter) # In[5]: time_text = '2019.12.13.AM 00:00' import pandas as pd pd.to_datetime(time_text, format='%Y.%m.%d.%p %H:%M').strftime('%Y-%m-%d %H:%M') # ## **2 Lambda** # In[6]: # Check if given numbers are in range using lambda function test = lambda x : True if (x > 10 and x < 20) else False print(test(12)) print(test(3)) print(test(24)) # https://thispointer.com/python-how-to-use-if-else-elif-in-lambda-functions/ # https://stackoverflow.com/questions/60261960/how-to-use-lambda-if-else-in-map-on-list-in-python list(map(lambda x : 'ok' if x == "apple" else None, ['apple', 'banana', 'cherry'])) # In[1]: test = { "A":1, "B":2, } # In[7]: test.get("d", 10) # In[3]: test