Python Tutorial: Indexing & resampling time series

Learn to convert dates to pandas datetime format, set them as your index, and slice time series by date ranges. Use frequency aliases like 'D' for daily or 'B' for business days to resample your data, which fills in missing dates with NaN values.
Chapters
In this chapter, you will learn about basic time series methods and transformations. These basic methods include parsing dates provided as strings and converting the result into the matching Pandas data type called datetime64. They also include selecting sub periods of your time series and setting or changing the frequency of the datetime index. You can change the frequency to a higher or lower value. Upsampling involves increasing the time frequency, which requires generating new data. Down sampling means decreasing the frequency, which requires aggregating data. We'll discuss this in the next chapter. Our first data set is a time series with two years of daily Google stock prices. You will often have to deal with dates that are of type object or string. You'll notice a column called date that is of data type object. However, when you print the first few rows using the head method, you see that it contains dates. To convert the strings to the correct data type, Pandas has the toDateTime function. Just pass a data column or series to this function and it will parse the string as DateTime64 type. You can now set the repaired column as index using setindex. The resulting datetime index lets you treat the entire dataframe as time series data. Plotting the stock price shows that Google has been doing well over these two years. It also shows that with a datetime index, Pandas automatically creates reasonably spaced date labels for the x axis. To select subsets of your time series, you can use strings that represent a complete date or relevant parts of a date. If you just pass a string representing a year, Pandas returns all dates within this year. If you pass a slice that starts with one month and ends at another, you get all dates within that range. Note that the date range will be inclusive of the end date, different from other intervals in Python. You can also use log with a complete date and a column label to select a specific stock price. You may have noticed that our datetime index did not have frequency information. You can set the frequency information using sfreq. The alias D stands for calendar day frequency. As a result, the DateTime index now contains many dates where stock wasn't bought or sold. These new dates have missing values. This is also called upsampling because the new data frame is of higher frequency than the original version. In the next chapter, you will learn how to create data points for the missing values. You can also convert the datetime index to Business day frequency. Pandas has a list of days commonly considered business days. The alias for Business day frequency is B. You now see a smaller number of additional dates created. You can use the method isnull to select the missing values and check which dates are considered business days but have no stock prices because no stocks were traded. Let's now practice your new time series kit.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







