DSC 201: Data Analysis & Visualization

[Pages:13]DSC 201: Data Analysis & Visualization

Time Series

Dr. David Koop

D. Koop, DSC 201, Fall 2016

Dates and Times

? What is time to a computer? - Can be stored as seconds since Unix Epoch (January 1st, 1970)

? Often useful to break down into minutes, hours, days, months, years...

? Lots of different ways to write time: - How could you write "November 29, 2016"? - European vs. American ordering...

? What about time zones?

D. Koop, DSC 201, Fall 2016

2

Python Support for Time

? The datetime package - Has date, time, and datetime classes

- .now() method: the current datetime - Can access properties of the time (year, month, seconds, etc.)

? Converting from strings to datetimes:

- datetime.strptime: good for known formats - dateutil.parser.parse: good for unknown formats ? Converting to strings

- str(dt) or dt.strftime() ? Differences between times

- datetime.timedelta: can get number of days/hours/etc. - deal with issues with different length months, etc.

D. Koop, DSC 201, Fall 2016

3

Pandas Support for Datetime

? pd.to_datetime: - convenience method - can convert an entire column to datetime

? Has a NaT to indicate a missing time value (like NaN but for timestamps)

? Stores in a numpy.datetime64 format ? pd.Timestamp: a wrapper for the datetime64 objects ? Can use time as an index ? Accessing a particular time or checking equivalence allows any

string that can be interpreted as a date: - ts['1/10/2011'] or ts['20110110']

D. Koop, DSC 201, Fall 2016

4

In [87]: hour Out[87]:

GYoeuncaenrdaetfiinnega mDualtitpele Rof aannogffesest by passing an integer:

In [88]: four_hours = Hour(4)

? index = pd.date_range('4/1/2012', '6/1/2012')

? CaInng[e89n]e:rafoteur_bhaousresd on a number of periods as well

Out[89]:

- index = pd.date_range('4/1/2012', periods=20)

?IinnFsrmteeqaodsuteuanspicnpygli(acfasrttieroiqnn)gs,cayolionaustrlowiklsoeuh'lHdo'wnoertvh'e4erHrn'a.enPedguetttoiinsegdxapinvliidicniettldeygecrrebaetfeoroentehoe fbtahseesfereoqb cr-eaCtoesdaesmfuolrtipsplee: cifying this (e.g. 4h, D, M)

- In [90]: pd.date_range('1/1/2000', '1/3/2000 23:59', freq='4h')

Out[90]: [2000-01-01 00:00:00, ..., 2000-01-03 20:00:00] Length: 18, Freq: 4H, Timezone: None

M-aCnyanofafslestos mcainx btheecmom: b'2inhe3d0tmog'ether by addition:

In [91]: Hour(2) + Minute(30) Out[91]:

Similarly, you can pass frequency strings like '2h30min' which will effectively be p to the same expression:

D. Koop,IDnS[C922]0:1, pFda.lld2a0t1e6_range('1/1/2000', periods=10, freq='1h30min')

5

Assignment 5

? ? Aggregation, resampling, and visualization of time series data ? Last assignment, but likely challenging...start now!

D. Koop, DSC 201, Fall 2016

6

Shifting dates with offsets

[102]: ts.shift(1, freq='90T')

[10T2T]h:iempeadnedaltsadate offsets can also be used with datetime or Ti

00--0012?--C3219oImn0011p[::u133te0003::d]00i:f00fefrerno-cm00e..sp08ab63ne68dt76aw43s89e.etnsedraiteess.offsets import Day, MonthEnd

00--0034?--L33i10vIen00s11i[::n133d000a4::]t00:e00tniomw--e00=m..15od11da77ut37lee89t85ime(2011,

pe: ?fddlioiIfafntff6.[4=d1a0py5as]r:sen_odwat+e(3"1*

Jan 2017")

Day()

11, 17)

- datetime.now().date()

? AlsOouta[p1d0.5]T:imTeidmeelsttaamopb(je'c2t01th1a-t1t1a-k2e0s0tr0in:g0s0::00')

datIefsy-woduiatthaedotfdifmsaeent.snaonwc(h).odraetde(o)ffs+etpldi.kTeiMmeodnetlhtEan(d",4thdeayfisr"s)t incremen

ndtao?sAtdhlsaeot,enReooxfllftdsdaettaestsecuasnicncgaoalrsndocihnbogeretduosoetffhdseewtsfriethqudeantceytirmueleo:r Timestamp

[103]: Ifnro[m10p6a]n:danso.wts+erMioenst.hoEfnfds(e)ts import Day, MonthEnd

Out[106]: Timestamp('2011-11-30 00:00:00')

[104]: now = datetime(2011, 11, 17)

In [107]: now + MonthEnd(2)

[105]: Onuotw[1+073]:* TDiamye(s)tamp('2011-12-31 00:00:00')

[10A5]n:chTiomreesdtaomfpf(s'e2t0s1c1a-1n1-e2x0pl0i0c:it0l0y:0"0r'o)ll" dates forward or back

D. Koop, DSC 201, Fall 2016

7

Time Zones

? Why? ? Coordinated Universal Time (UTC) is the standard time (basically

equivalent to Greenwich Mean Time (GMT) ? Other time zones are UTC +/- a number in [1,12] ? Dartmouth is UTC-5 (aka US/Eastern)

D. Koop, DSC 201, Fall 2016

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download