Path: blob/master/lessons/lesson_16/01_time_series (done).ipynb
2363 views

Working With Time Series Data
Learning Objectives
After this lesson, you will be able to:
Identify time series data.
Explain the challenges of working with time series data.
Use the
datetimelibrary to represent dates as objects.Preprocess time series data with Pandas.
Lesson Guide
Time Series Data
A time series is a series of data points that's indexed (or listed, or graphed) in time order. Most commonly, a time series is a sequence that's taken at successive equally spaced points in time. Time series are often represented as a set of observations that have a time-bound relation, which is represented as an index.
Time series are commonly found in sales, analysis, stock market trends, economic phenomena, and social science problems.
These data sets are often investigated to evaluate the long-term trends, forecast the future, or perform some other form of analysis.
Check for Understanding: List some examples of real-world time series data.
Let's take a look at some Apple stock data to get a feel for what time series data look like.
Take a high-level look at the data. What are we looking at?
As time is important to time series data, we will need to interpret these data in the ways that humans interpret them (which is many ways).
Python's DateTime library is great for dealing with time-related data, and Pandas has incorporated this library into its own datetime series and objects.
In this lesson, we'll review these data types and learn a little more about each of them:
datetimeobjects.datetimeseries.Timestamps.
timedelta().
datetime Objects
Below, we'll load in the DateTime library, which we can use to create a datetime object by entering in the different components of the date as arguments.
The components of the date are accessible via the object's attributes.
timedelta()
Suppose we want to add time to or subtract time from a date. Maybe we're using time as an index and want to get everything that happened a week before a specific observation.
We can use a timedelta object to shift a datetime object. Here's an example:
datetime's .now() function will give you the datetime object of this very moment.
The current time is particularly useful when using timedelta().
Note: The largest value a timedelta() can hold is days. For instance, you can't say you want your offset to be two years, 44 days, and 12 hours; you have to convert those years to days.
You can read more about the timedelta() category here.
Guided Practice: Apple Stock Data
We can practice using datetime functions and objects using Apple stock data.
The Date column starts off as an object.
Convert time data to a datetime object.
Overwrite the original Date column with one that's been converted to a datetime series.
We can see these changes reflected in the Date column structure.
We can also see that the Date object has changed.
The .dt Attribute
Pandas' datetime columns have a .dt attribute that allows you to access attributes that are specific to dates. For example:
And, there are many more!
Check out the Pandas .dt documentation for more information.
Timestamps
Timestamps are useful objects for comparisons. You can create a timestamp object using the pd.to_datetime() function and a string specifying the date. These objects are especially helpful when you need to perform logical filtering with dates.
The main difference between a datetime object and a timestamp is that timestamps can be used as comparisons.
Let's use the timestamp ts as a comparison with our Apple stock data.
We can even get the first and last dates from a time series.
Check for Understanding: Why do we convert the DataFrame column containing the time information into a
datetimeobject?
Set datetime to Index the DataFrame
After converting the column containing time data from object to datetime, it is also useful to make the index of the DataFrame a datetime.
Let's set the Date column as the index.
Filtering by Date with Pandas
It is easy to filter by date using Pandas. Let's create a subset of data containing only the stock prices from 2017. We can specify the index as a string constant.
There are a few things to note about indexing with time series. Unlike numeric indexing, the end index will be included. If you want to index with a range, the time indices must be sorted first.
Recap: The steps for preprocessing time series data are to:
Convert time data to a
datetimeobject.Set
datetimeto index the DataFrame.
Recap
We use time series analysis to identify changes in values over time.
The
datetimelibrary makes working with time data more convenient.To preprocess time series data with Pandas, you:
Convert the time column to a
datetimeobject.Set the time column as the index of the DataFrame.
Instructor Note: These are optional and can be assigned as student practice questions outside of class.