![时间序列 python](https://i-blog.csdnimg.cn/blog_migrate/a88904bc8dd512463daf14e7831322c4.png)
时间序列 python
Python-时间序列 (Python - Time Series)
Time series is a series of data points in which each data point is associated with a timestamp. A simple example is the price of a stock in the stock market at different points of time on a given day. Another example is the amount of rainfall in a region at different months of the year.
时间序列是一系列数据点,其中每个数据点都与时间戳关联。 一个简单的例子是在给定的一天中,股票在不同时间点的价格。 另一个例子是该地区一年中不同月份的降雨量。
In the below example we take the value of stock prices every day for a quarter for a particular stock symbol. We capture these values as a csv file and then organize them to a dataframe using pandas library. We then set the date field as index of the dataframe by recreating the additional Valuedate column as index and deleting the old valuedate column.
在下面的示例中,我们以特定股票代码每天四分之一的股价价格为例。 我们将这些值捕获为一个csv文件,然后使用pandas库将它们组织到一个数据框中。 然后,通过将其他Valuedate列重新创建为索引并删除旧的valuedate列,将date字段设置为数据框的索引。
样本数据 (Sample Data)
Below is the sample data for the price of the stock on different days of a given quarter. The data is saved in a file named as stock.csv
以下是给定季度不同日期的股票价格示例数据。 数据保存在名为stock.csv的文件中
ValueDate Price
01-01-2018, 1042.05
02-01-2018, 1033.55
03-01-2018, 1029.7
04-01-2018, 1021.3
05-01-2018, 1015.4
...
...
...
...
23-03-2018, 1161.3
26-03-2018, 1167.6
27-03-2018, 1155.25
28-03-2018, 1154
创建时间序列 (Creating Time Series)
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('path_to_file/stock.csv')
df = pd.DataFrame(data, columns = ['ValueDate', 'Price'])
# Set the Date as Index
df['ValueDate'] = pd.to_datetime(df['ValueDate'])
df.index = df['ValueDate']
del df['ValueDate']
df.plot(figsize=(15, 6))
plt.show()
Its output is as follows −
其输出如下-
![timeseries.png](https://i-blog.csdnimg.cn/blog_migrate/2ecd87d1565a4ec2f9f4ca47a85e4ce4.png)
翻译自: https://www.tutorialspoint.com/python_data_science/python_time_series.htm
时间序列 python