Time Series Basics with Pandas

In my last post, I mentioned multiple selecting and filtering in Pandas library. I will talk about time series basics with Pandas in this post. Time series data in different fields such as finance and economy is an important data structure. The measure…


This content originally appeared on Level Up Coding - Medium and was authored by Tirendaz Academy

In my last post, I mentioned multiple selecting and filtering in Pandas library. I will talk about time series basics with Pandas in this post. Time series data in different fields such as finance and economy is an important data structure. The measured or observed values over time are in a time series structure. Pandas is very useful for time series analysis. There are tools that we can easily analyze.

Photo by John Schnobrich on Unsplash

In this article, I will explain the following topics.

  • What is the time series?
  • What are time series data structures?
  • How to create a time series?
  • What are the important methods used in time series?

What is the time series?

Many time series are fixed frequency, meaning that data points in the time series consist of fixed intervals such as every minute or every day or 1 week. The time series can also consist of irregular intervals. Time series data can consist of a date in time. This is called time stamps. For example, a date such as 15 April 2019 at 10:00 is timestamped.

Python has modules such as datetime that do operations on date and time, but Pandas with useful tools is used more often for time series data analysis. At the same time, Pandas coordinates the relationship between libraries for time series analysis.

Pandas’ time series tools are very useful when data is time-stamped. The simplest of the time series is the Series structure indexed by timestamp.

First, let’s import Pandas with pd, Numpy with np, and from datetime module to datetime.

Let’s create date values now.

Let’s create a Series structure whose index is this date object.

Let’s check the index of this time series.

Time Series Data Structures

Three types of structures are used to work with the time series.

The first of these is the Timestamps type. This structure is basically similar to Python’s datetime module. But it is more useful. The index structure associated with this type is DatetimeIndex.

The second is the time period type. The period consists of a time span with start and end dates. This type is based on fixed interval dates. The index structure associated with this type is PeriodIndex.

The third is timedelta, ie time delta or duration type. Shows the time between two dates. The index structure associated with this type is TimedeltaIndex.

The most used of these types are Timestamps and DatetimeIndex objects. To convert a single date to Timestamps, the function pd.to_datetime () is used.

The pd.to_datetime () function converts date series of different formats into DatetimeIndex object.

Thus, different format dates are converted into DatetimeIndex object. Any DatetimeIndex to_period () function can be converted to PeriodIndex by adding frequency codes.

For example “D” indicates the daily frequency.

A TimedeltaIndex structure can be created by subtracting another date from one date to another.

Creating a Time Series

Pandas has various functions to create regular date series. pd.date_range () for timestamps pd.period_range () for the period pd.timedelta_range () is used to create time delta type data.

As you know, range () in Python and np.arange () functions in Numpy take the starting point, endpoint, and step number parameters. Likewise, the pd.date_range () function takes the start date, end date, and optional frequency code to create a regular date series. For example;

Alternatively, the date range can be created using the period option. For this, we only need to enter the starting date.

We can also use the freq argument while creating a date range. By default, the frequency of the date_range function is daily. If we want to take the frequency as hours, the value “H” representing the hour is used.

Creating a time period and time delta is similar with the functions pd.period_range () and pd.timedelta_range(). For example;

Thus, monthly frequency dates were written on the screen. Let’s want to create a time series with one hour increment;

The series-shaped time series is like the Series structure of Pandas. Time series can be indexed and selected. For example, let’s assign index 1 of the time series to a variable.

Now let’s call the value of this stamp.

The date value written as a string is also read as a date index.

or

We can play like a ball with long time series.

Let’s take the dates for the year 2020.

The string ‘2020’ here has been interpreted as a year. If you want, you can make this year specific with a month.

You can slice the dates with datetime if you want.

The Important Methods Used in Time Series

Values between two dates can be sliced with the truncate method. For example, let’s see the dates until 1/15/2020. Let’s print ts on the screen first.

Let’s use truncate() method.

The operations done with the time series in the Series structure until now are also valid for the DataFrame structure. First, let’s create a date variable with the date_range method.

Let’s create a dataframe data set that indexes this date variable.

Let’s see the values in the 10th month of 2020 in this data set.

In some cases, the dates can be repeated.

It can be checked whether there is a repeating index in the data or not with the is_unique attribute.

False indicates that there are repeating indexes. Let’s want to group recurring dates.

Let’s find out how many times the grouped dates repeat.

Let’s find the means of the grouped dates.

You can access the notebook I used for this post on our GitHub page.

That’s it. I hope you enjoy this post. In my next blog post, I will talk about working with indexes in time series.

Please clap if you like this blog post. Also, don’t forget to follow us on our Tirendaz Academy YouTube channel and Medium page.

See you in the next blog post …


Time Series Basics with Pandas was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Tirendaz Academy


Print Share Comment Cite Upload Translate Updates
APA

Tirendaz Academy | Sciencx (2021-02-25T01:09:48+00:00) Time Series Basics with Pandas. Retrieved from https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/

MLA
" » Time Series Basics with Pandas." Tirendaz Academy | Sciencx - Thursday February 25, 2021, https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/
HARVARD
Tirendaz Academy | Sciencx Thursday February 25, 2021 » Time Series Basics with Pandas., viewed ,<https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/>
VANCOUVER
Tirendaz Academy | Sciencx - » Time Series Basics with Pandas. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/
CHICAGO
" » Time Series Basics with Pandas." Tirendaz Academy | Sciencx - Accessed . https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/
IEEE
" » Time Series Basics with Pandas." Tirendaz Academy | Sciencx [Online]. Available: https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/. [Accessed: ]
rf:citation
» Time Series Basics with Pandas | Tirendaz Academy | Sciencx | https://www.scien.cx/2021/02/25/time-series-basics-with-pandas/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.