Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_16/solution-code/01_time_series_solutions.ipynb
1904 views
Kernel: Python 2

Working With Time Series Data


Independent Practice

Instructor Note: These are optional and can be assigned as student practice questions outside of class.

1) Create a datetime object representing today's date.

from datetime import datetime from datetime import timedelta # Time this lesson plan was written: lesson_date = datetime(2018, 2, 25, 23, 31, 1, 844089)
print(lesson_date)
2018-02-25 23:31:01.844089

2) Load the UFO data set from the internet.

import pandas as pd from datetime import timedelta %matplotlib inline ufo = pd.read_csv('http://bit.ly/uforeports')
ufo.head()
ufo.dtypes
City object Colors Reported object Shape Reported object State object Time object dtype: object

3) Convert the Time column to a datetime object.

ufo['Time'] = pd.to_datetime(ufo.Time)
ufo.head()
ufo.dtypes
City object Colors Reported object Shape Reported object State object Time datetime64[ns] dtype: object

4) Set the Time column to the index of the DataFrame.

ufo.set_index('Time', inplace=True)
ufo.head()

5) Create a timestamp object for the date January 1, 1999.

ts = pd.to_datetime('1/1/1999')

6) Use the timestamp object to perform logical filtering on the DataFrame and create a subset of entries with a date above or equal to January 1, 1999.

recent_ufos = ufo.loc[ufo.index >= ts, :]
recent_ufos.head()