Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize a time series in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to achieve a time series in Python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

A time series is indexed (or listed or plotted) by a series of data points in time. The most common is that time series are obtained at continuous intervals of time. Therefore, it is a series of discrete-time data. Examples of time series include the height of the tide, the number of sunspots and the daily closing price of the Dow Jones Industrial average.

We will see some important points that can help us analyze any time series dataset. These are:

Correctly load the time series dataset in Pandas

Time series data index

Time resampling using Pandas

Rolling time series

Using Pandas to draw time series data

Correctly load the time series dataset in Pandas

Let's load the above dataset in Pandas.

Since we want to use the "DATE" column as an index, just by reading, we have to add some additional parameters.

Great, now let's add the DATE column as an index, but let's check its data type to see whether pandas handles the index as a simple object or as pandas's built-in DateTime data type.

Great, now let's add the DATE column as an index, but let's check its data type to see whether pandas handles the index as a simple object or as pandas's built-in DateTime data type.

Here, we can see that Pandas treats the Index column as a simple object, so let's convert it to DateTime. We can do the following:

Now we can see that the dtype of our dataset is datetime64 [ns]. This "[ns]" indicates that its accuracy is nanosecond. We can change it to "day" or "month" if necessary.

In addition, to avoid these troubles, we can use Pandas to load data in a single line of code, as shown below.

Here, we added parse_dates = True, so it will automatically use our index as the date.

Time series data index

For example, all the data I want to get is from 2000-01-01 to May 1, 2015. To do this, we can simply use indexes in Pandas like this.

Here we provide data for all months from 2000-01-01 to 2015-01-01.

For example, we want all the data in the first few months from 1992-01-01 to 2000-01-01. We can do this simply by adding another parameter, which is similar to adding a step parameter when slicing a list in python.

In Pandas, this syntax is ['starting date':'end date':step]. Now, if we look at the dataset, it is in monthly format, so we need data every 12 months from 1992 to 2000. We can operate in the following ways.

The above is how to achieve a time series in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report