Important Methods for Time Series in Pandas

How to use the resample, shift, and windows methods using the Facebook stock dataset, and how to work with time zones.

Tirendaz AI
Level Up Coding

--

Photo by Mimi Thian on Unsplash

A time series is a series of data points indexed in time order. Time series analysis is very frequently used in finance studies. Pandas is a very important library for time series analysis. With Pandas, you can easily manipulate your time series. In this post, I’ll cover the following topics:

  • What is resampling?
  • What is shifting?
  • Windowing operations
  • Handling time zone
  • Changing the time zone

Let’s dive in!

What is Resampling?

In Pandas, resample is a time-based groupby, followed by a reduction method on each of its groups. With resampling, you can convert the time series from one frequency to another. To show resampling, let me import Pandas and Numpy libraries.

The resample performs grouping and is similar to the groupby technique. To explain this, I’m going to use the Facebook stock dataset. You can find this dataset here. Let’s import this dataset.

Notice that I used the parse_dates parameter to convert the Date column to a datetime object. I also set the index_col parameter to specify this column as an index object. Let’s look at the first rows of the data set.

The resample method is very flexible and allows you to specify many different parameters to control the frequency conversion and resampling operation. Let’s take a look at the monthly mean values with the resample method.

Let me draw the closing prices. First, I’m going to use the % Matplotlib inline magic command to see the plots between lines.

To see the plot of monthly mean values, you can use the plot method.

Let’s take a look at the bar graph of the mean prices of the quarter values.

What is shifting?

You may want to shift or lag the values in a time series back and forward in time using the shift method. Both Series and DataFrame have the shift attribute. Let’s take the values of the Facebook closing prices for March and assign these values to the fb1 variable.

Let’s take a look at the fb1 variable.

The shift method accepts the freq parameter that can accept a DataOffset class or other timedelta like an object or an offset alias. When you can determine the freq parameter, the shift method changes all the dates in the index. Let’s shift the values in a time series two forward in time.

Let’s shift the values in a time series two back in time.

You can see this change in the same table.

Let’s have a look at the fb1 variable again.

In the time series, you sometimes want to find the percentage of change in one day. Now let’s find the one-day difference in the values.

Let’s have a look at the fb1 variable again.

The tshift method shifts the time index, using the index’s frequency. This method plays the index forward or backward. To show this, let’s just take the Close column first.

Let’s select the Close column.

Let’s take a look at the first rows of the fb2 variable.

Let’s have look at the index structure of this data.

Note that date values don’t have a frequency value. Let’s assign frequencies to these indexes. I’m going to use the date_range function for this.

Now let’s look at the indexes of the fb2 data again.

Notice that the dates refer to working days. You can move the indexes back and forward. For example, let’s move the daily index forward.

Let’s go back for 2 days.

Windowing operations

Pandas includes a compact set of APIs for performing windowing operations, an operation that performs an aggregation over a sliding partition of values. You can think of the API functions similarly to the groupby API.

Window operations are used to smooth data. Let’s rethink the Facebook stock dataset and discuss the rolling function. This function is like the groupby and resample methods. You can group it with the rolling function. For example, rolling (30) implies the mean of 30 days. To illustrate this, let’s first plot the fb closing prices.

Now let’s find the 30-day means with the rolling function and show these values on the plot.

Handling Time Zone

As you know, each region in the world has a different time zone. Many researchers work according to the abbreviation UTC international time.

The pytz library is used in the time zone in Python. This library comes loaded with Anaconda. If your computer doesn’t install this library, you can install it with pip or conda. Let’s import this library.

For example, let’s learn Turkey’s time zone.

Let’s find the time zone of the US New York region.

For example, let’s find places that are 7 hours behind the central time zone.

Changing the time zone

You can change the time zone of your dataset. To show this, let’s create a date object.

Let’s create a time series with this date object.

Let’s take a look at this variable.

Let’s use the tz attribute to control the time zone of the index.

The ts data no have a time zone. Now let’s assign the date ranges to this data. To do this, you can use the tz_localize method.

If you want to arrange the time series according to a special region, you can use the tz_convert method. For example, let’s set the time series according to Hawaii.

You can transform your dataset to the international central time zone.

Let’s convert this time to international local time.

Now let’s convert this local time to Istanbul local time.

If two time series in different regions are combined, the results are shown in central-local time. Let’s print the ts time series again.

Let’s create two time series with two different region times.

Let’s add up this time series now.

Let’s take a look at the indexes of the result variable.

Thus, the result indexes were printed according to the central-local time.

Conclusion

Time series analysis is very frequently used in finance studies. In this post, I talked about how to use the resample, shift, and windows methods, and how to work with time zones. That’s it. I hope you enjoy it. Thank you for reading. You can find this notebook here.

Don’t forget to follow us on YouTube | GitHub | Twitter | Kaggle | LinkedIn

If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇

--

--