import datetime
datetime.datetime.now()
delta = datetime.datetime(2020, 5, 30) - datetime.datetime(1938, 6, 3)
delta
import pandas as pd
import numpy as np
x = datetime.datetime(2020, 5, 17)
x
'2016 Jul 1', '7/1/2016', '1/7/2016', 'July 1, 2016', '2016-07-01', '2016/07/01'
# နာရီ မိနစ် စက္ကန့် ထည့်သွင်း ဖော်ပြသည်။
pd.Timestamp('2020-07-10')
# နာရီ မိနစ် စက္ကန့် ထည့်သွင်း ဖော်ပြသည်။
pd.Timestamp('2016-07-10 10')
# နာရီ မိနစ် စက္ကန့် ထည့်သွင်း ဖော်ပြသည်။
pd.Timestamp('2016-07-10 10:15')
hint: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#time-date-components
t = pd.Timestamp('2016-07-10 10:15')
pd.Period('2016-01') # လကိုသာ ဆိုလိုသည်။
pd.Period('2016-01-02') # နေ့အထိ ဖော်ပြသည်။
pd.Period('2016-01-01') # နေ့အထိ ဖော်ပြသည်။
pd.Period('2016-01-01 10') # နာရီ အထိ ဖော်ပြသည်။
pd.Period('2016-01-01 10:10') # မိနစ်အထိ ဖော်ပြသည်။
pd.Period('2016-01-01 10:10:10') #စက္ကန့်အထိ ဖော်ပြသည်။
# TIME OFFSETS
pd.Timedelta('1 day') # ၁ရက်
pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day') # '2016-01-01 10:10' မှ ၁ရက်တိတိကြာပြီးချိန်
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day') # '2016-01-01 10:10' မှ ၁ရက်တိတိကြာပြီးချိန်
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns') # '2016-01-01 10:10' မှ 15 နာနို စက္ကန့်အကြာ
#Please ignore below code
from IPython.core.interactiveshell import InteractiveShell # notebook setting အတွက်
InteractiveShell.ast_node_interactivity = "all" # notebook setting အတွက်
pandas မှ date_range()ကို အသုံးပြု၍ ရက်စွဲများ၊ နေ့ရက်များ၊ အချိန်များကို လိုက်ဖြစ်အောင် ပြင်ဆင်နိုင်သည်။ pandas မှ date_range() က အောက်ပါ အတိုင်းဖြစ်သည်။
date_range(
start=None,
end=None,
periods=None,
freq=None,
tz=None,
normalize=False,
name=None,
closed=None,
**kwargs,
)
စမည့်ရက်(start='2020 Jul 1')ထည့်ပေးရသည်။
ကြာမည့်အချိန်ကာလ(periods = 10)ထည့်ပေးရသည်။ စုစုပေါင်း ၁၀ ရက်
ပိုင်းခြားရမည့် ပမာဏ, စက္ကန့်တိုင်း, နာရီတိုင်း၊ နေ့စဥ်, လစဥ်, နှစ်စဥ်, ရုံဖွင့်ရက်တိုင်း စသည်ဖြင့် ထည့်ပေးရသည်။
freq = 'D' သည် frequency = daliy ဖြစ်သည်။ D= daliy
rng = pd.date_range('2020 Jul 1', periods = 10, freq = 'D')
rng
စမည့်ရက်(start='2020 Jul 1')ထည့်ပေးရသည်။
ကြာမည့်အချိန်ကာလ(periods = 12)ထည့်ပေးရသည်။ စုစုပေါင်း ၁၂ လ
ပိုင်းခြားရမည့် ပမာဏ, စက္ကန့်တိုင်း, နာရီတိုင်း၊ နေ့စဥ်, လစဥ်, နှစ်စဥ်, ရုံဖွင့်ရက်တိုင်း စသည်ဖြင့် ထည့်ပေးရသည်။
freq = 'M' သည် frequency = daliy ဖြစ်သည်။ D= daliy
m_range = pd.date_range('2020 Jul 1', periods = 12, freq = 'M')
m_range
စမည့်ရက် နှင့် နောက်ဆုံးရက် ထည့်ပေးလျှင်လည်းရသည်။ စမည့်ရက် နှင့် နောက်ဆုံးရက် ထည့်ပေးလျှင် freq = 'D' သည် daliy frequency သည် default ဖြစ်သည်။ D= daliy ဟု ဖော်ပြပေးသည်။
pd.date_range(start='1/1/2018', end='1/08/2018')
စမည့်ရက်(start='1/1/2018') နှင့် ကြာမည့်အချိန်ကာလ(periods = 8)ထည့်ပေးရသည်။ စုစုပေါင်း 8 ရက်
pd.date_range(start='1/1/2018', periods=8)
နောက်ဆုံးရက်(end='2020 Jul 1')နှင့် ကြာမည့်အချိန်ကာလ(periods = 8)ထည့်ပေးရသည်။ စုစုပေါင်း 8 ရက်
pd.date_range(end='2020 Jul 1', periods=8)
'2018-04-24'မှ စ၍ (၁) ရက်ခြားစီ ဆေးခန်း (၃)ကြိမ် ပြရမည်။
pd.date_range(start='2018-04-24', periods=3, freq = '2D')
ရက်တွေ လတွေ နှစ်တွေကို မြှောက်ဖော်ကိန်းဖြင့် ရေးနိုင်သည်။
3M သည် ၃လ ဖြစ်သည်။ 7D သည် တစ်ပတ်ဖြစ်သည်။
1/7/2020 နေ့မှ စ၍ လကုန်ရက်တိုင်း (၃)လစာ ပင်စင်ထုတ်ရမည်။ တစ်နှစ်စာ ဖြစ်သည်။
pd.date_range(start='1/7/2020', periods=4, freq='3M') # 3M = ၃လ
pd.date_range(start='1/1/2018', periods=5, freq='3M')
freq=pd.offsets.MonthEnd(3)) သည် ၃လ တစ်ခါဖြစ် သည်။
pd.date_range(start='1/1/2018', periods=5, freq=pd.offsets.MonthEnd(3))
B သည် business day frequency ဖြစ်သည်။ ရုံးဖွင့်ရက်တိုင်း ( စနေ တနင်္ဂနွေ မပါ) freq=pd.offsets.BDay())
pd.date_range(start='1/1/2018', end='1/10/2018', freq=pd.offsets.BDay())
မိမိ လိုချင်သည့် timezone ကို လည်း tz ဖြင့် ရေးနိုင်(Specify လုပ်)သည်။
pd.date_range(start='1/1/2018', periods=5, tz='Asia/Singapore')
closed ဖြင့် အစဆုံးရက် နှင့် နောက်ဆုံးရက် ပါ မပါ control လုပ်နိုင်သည်။ ဘာမှ မပြောထားလျှင် (default) အစဆုံးရက် နှင့် နောက်ဆုံးရက်ကို ထည့်ထားးသည်။ (The default includes boundary points on either end.)
pd.date_range(start='2017-01-01', end='2017-01-04', closed=None)
closed='left' သည် နောက်ဆုံရက်ကို ချန်ထားသည်။ ထည့်မ တွက်ပါ။ ( to exclude end if it falls on the boundary.)
pd.date_range(start='2017-01-01', end='2017-01-04', closed='left')
closed='right' သည် အစဆုံးရက်ကို ချန်ထားသည်။ ထည့်မ တွက်ပါ။ (to exclude start if it falls on the boundary.)
pd.date_range(start='2017-01-01', end='2017-01-04', closed='right')
# ရုံးဖွင့်ရက်များသာ (Only want business days)
pd.period_range('2016-01-01 10:10', freq = 'B', periods = 10)
frequency များကို ပေါင်းရေးနိုင်သည်။ ဥပမာ ၂၅ နာရီကို ၁ ရက်နှင့် ၁နာရီ ဟု ရေးနိုင်သည်။ What if you want to advance by 25 hours each day. freq = '25H' သည် ၂၅ နာရီ
p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)
p1
p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)
p2
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases Time objects ကိုလည်း Indexing လုပ်နိုင်သည်။ date range ကို သုံးထားသည်။
rng = pd.date_range('2020 Jul 1', periods = 10, freq = 'D')
rng
pd.Series(range(len(rng)), index = rng)
ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods = 10, freq = 'H'))
ts
ts_period = ts.to_period()
ts_period
Offset aliases
A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases.
Alias Description
B = business day frequency
C = custom business day frequency
D = calendar day frequency
W =weekly frequency
M = month end frequency
SM =semi-month end frequency (15th and end of month)
BM =business month end frequency
CBM= custom business month end frequency
MS= month start frequency
SMS= semi-month start frequency (1st and 15th)
BMS= business month start frequency
CBMS= custom business month start frequency
Q = quarter end frequency
BQ = business quarter end frequency
QS = quarter start frequency
BQS =business quarter start frequency
A, Y = year end frequency
BA, BY = business year end frequency
AS, YS = year start frequency
BAS, BYS = business year start frequency
BH= business hour frequency
H =hourly frequency
T, min = minutely frequency
S = secondly frequency
L, ms =milliseconds
U, us = microseconds
N = nanoseconds
import datetime
dti1 = pd.to_datetime(['1/1/2018'])
dti2 = pd.to_datetime([np.datetime64('2018-01-01')])
dti3 = pd.to_datetime([datetime.datetime(2018, 1, 1)])
dti = pd.to_datetime(['1/1/2018', np.datetime64('2018-01-01'),
datetime.datetime(2018, 1, 1)])
dti1
dti2
dti3
dti
import calendar as cal
pd.date_range(start='7/1/2012', end='7/10/2012',
freq=pd.offsets.CDay(calendar=cal)).to_pydatetime()