沧州市网站,wordpress专题,外汇交易网站建设,廊坊网站建设廊坊网络公司驻梦目录
# 开篇
1. 创建和操作时间序列对象
2. 时间序列数据的读取和存储
3. 时间序列数据的索引和切片
4. 时间序列数据的操作和转换
5. 时间序列数据的可视化
6. 处理时间序列中的缺失值
7. 时间序列数据的聚合和分组
8. 时间序列的时间区间和偏移量操作
示例代码
运行结果 # 开篇 在Python中时间序列是一种特殊的数据类型通常用于表示一系列按时间顺序排列的数据点。时间序列可以是一维数组其中每个数据点都和一个特定的时间点相关联。
时间序列在数据分析和预测中非常重要因为它们可以帮助我们理解数据随时间变化的模式和趋势。通过对时间序列数据进行分析我们可以发现周期性变化、趋势、季节性等特征并基于这些特征进行预测和决策。 在Python中有许多用于处理时间序列数据的库如pandas、numpy、matplotlib等。这些库提供了丰富的功能和工具可以帮助我们加载、处理、可视化和分析时间序列数据。
总的来说时间序列在Python中的作用主要包括
数据分析和预测通过对时间序列数据进行分析可以揭示数据的模式和趋势从而进行预测和决策。可视化通过绘制时间序列图表可以直观地展示数据随时间的变化帮助我们理解数据的特征和规律。数据处理和转换可以利用时间序列数据进行数据清洗、转换和处理以便更好地进行后续的分析和建模工作。特征工程时间序列数据可以用于构建特征帮助机器学习模型更好地理解数据和进行预测。 总的来说时间序列在Python中是一种非常重要的数据类型可以帮助我们更好地理解和分析数据从而做出更准确的预测和决策。
1. 创建和操作时间序列对象
使用 pd.date_range、pd.to_datetime 和 pd.Timestamp 创建时间索引。DatetimeIndex 是 Pandas 时间序列的核心。
import pandas as pd# 创建时间序列
date_rng pd.date_range(start2023-01-01, end2023-06-30, freqD)
data pd.DataFrame(date_rng, columns[日期])
data.set_index(日期, inplaceTrue)2. 时间序列数据的读取和存储
使用 read_csv、read_excel 等方法读取带有日期时间数据的文件并使用 parse_dates 参数进行解析。保存时间序列数据到文件中例如使用 to_csv、to_excel 等。
# 读取时间序列数据
data pd.read_csv(data.csv, parse_dates[日期], index_col日期)# 保存时间序列数据
data.to_csv(output.csv)3. 时间序列数据的索引和切片
基于时间索引的切片和子集选择例如 data[2023-01-01:2023-02-01]。使用 resample 方法进行重采样。
# 时间索引切片
subset data[2023-01-01:2023-02-01]# 重采样
monthly_data data.resample(M).mean()4. 时间序列数据的操作和转换
使用 shift、rolling 和 expanding 方法进行移动窗口操作和滚动计算。使用 diff 计算差分。使用 resample 进行频率转换和聚合操作。
# 滚动计算
data[7天滚动平均] data[值].rolling(window7).mean()# 差分计算
data[差分] data[值].diff()# 重采样聚合
weekly_data data.resample(W).sum()5. 时间序列数据的可视化
使用 ECharts通过 pyecharts绘制时间序列数据的图表。
from pyecharts.charts import Line
from pyecharts import options as opts# 时间序列数据的可视化
line (Line(init_optsopts.InitOpts(width1000px, height600px)) # 设置图表的宽度和高度.add_xaxis(data.index.strftime(%Y-%m-%d).tolist()).add_yaxis(每日温度, data[温度].tolist(), is_smoothTrue, label_optsopts.LabelOpts(is_showFalse)).add_yaxis(7天滚动平均, data[7天滚动平均].tolist(), is_smoothTrue, label_optsopts.LabelOpts(is_showFalse)).set_global_opts(title_optsopts.TitleOpts(title温度时间序列数据,pos_top5%,pos_leftcenter),xaxis_optsopts.AxisOpts(type_category,axislabel_optsopts.LabelOpts(rotate-15) # 旋转x轴标签),yaxis_optsopts.AxisOpts(type_value),tooltip_optsopts.TooltipOpts(triggeraxis),legend_optsopts.LegendOpts(pos_top10%, # 设置图例的位置pos_leftcenter,orienthorizontal))
)
6. 处理时间序列中的缺失值
使用 fillna、interpolate 和 dropna 方法处理缺失数据。
# 填充缺失值
data[值].fillna(methodffill, inplaceTrue)# 插值填充
data[值].interpolate(methodlinear, inplaceTrue)# 删除缺失值
data.dropna(inplaceTrue)7. 时间序列数据的聚合和分组
使用 groupby 和 resample 方法对时间序列数据进行分组和聚合。
# 按月分组聚合
monthly_grouped data.groupby(data.index.month).sum()# 重采样聚合
quarterly_data data.resample(Q).mean()8. 时间序列的时间区间和偏移量操作
使用 pd.offsets 模块中的类进行自定义时间偏移量操作。
# 自定义时间偏移量
data.index data.index pd.offsets.Day(1)示例代码
import pandas as pd
import numpy as np
from pyecharts.charts import Line
from pyecharts import options as opts# 1. 创建时间序列数据
date_rng pd.date_range(start2023-01-01, end2023-06-30, freqD)
data pd.DataFrame(date_rng, columns[日期])
print(\n日期:)
print(data)
data[温度] np.random.randint(0, 35, size(len(date_rng)))
print(\n温度:)
print(data)
data.set_index(日期, inplaceTrue)# 2. 读取和存储时间序列数据
data.to_csv(temperature_data.csv)
data pd.read_csv(temperature_data.csv, parse_dates[日期], index_col日期)# 3. 时间序列数据的索引和切片
subset data[2023-01-01:2023-02-01]
monthly_data data.resample(ME).mean() # 修改 M 为 ME# 4. 时间序列数据的操作和转换
data[7天滚动平均] data[温度].rolling(window7).mean()
data[温度差分] data[温度].diff()
weekly_data data.resample(W).mean()
print(\n7天滚动平均/温度差分:)
print(data)# 5. 时间序列数据的可视化
line (Line(init_optsopts.InitOpts(width1000px, height600px)) # 设置图表的宽度和高度.add_xaxis(data.index.strftime(%Y-%m-%d).tolist()).add_yaxis(每日温度, data[温度].tolist(), is_smoothTrue, label_optsopts.LabelOpts(is_showFalse)).add_yaxis(7天滚动平均, data[7天滚动平均].tolist(), is_smoothTrue, label_optsopts.LabelOpts(is_showFalse)).set_global_opts(title_optsopts.TitleOpts(title温度时间序列数据,pos_top5%,pos_leftcenter),xaxis_optsopts.AxisOpts(type_category,axislabel_optsopts.LabelOpts(rotate-15) # 旋转x轴标签),yaxis_optsopts.AxisOpts(type_value),tooltip_optsopts.TooltipOpts(triggeraxis),legend_optsopts.LegendOpts(pos_top10%, # 设置图例的位置pos_leftcenter,orienthorizontal))
)
line.render(temperature_time_series.html)# 6. 处理时间序列中的缺失值
data.loc[2023-02-15:2023-02-20, 温度] np.nan
data.loc[:, 温度] data[温度].ffill() # 使用 obj.ffill() 替换 fillna
data.loc[:, 温度] data[温度].interpolate(methodlinear)
print(\n处理缺失值: )
print(data)# 7. 时间序列数据的聚合和分组
monthly_grouped data.groupby(data.index.month).mean()
quarterly_data data.resample(QE).mean() # 修改 Q 为 QE
print(\n月度数据:)
print(monthly_grouped)
print(\n季度数据:)
print(quarterly_data)# 8. 时间序列的时间区间和偏移量操作
data.index data.index pd.offsets.Day(1)
print(\n时间区间和偏移量操作:)
print(data)
运行结果
日期:日期
0 2023-01-01
1 2023-01-02
2 2023-01-03
3 2023-01-04
4 2023-01-05
.. ...
176 2023-06-26
177 2023-06-27
178 2023-06-28
179 2023-06-29
180 2023-06-30[181 rows x 1 columns]温度:日期 温度
0 2023-01-01 25
1 2023-01-02 10
2 2023-01-03 24
3 2023-01-04 23
4 2023-01-05 17
.. ... ..
176 2023-06-26 22
177 2023-06-27 29
178 2023-06-28 6
179 2023-06-29 6
180 2023-06-30 24[181 rows x 2 columns]7天滚动平均/温度差分:温度 7天滚动平均 温度差分
日期
2023-01-01 25 NaN NaN
2023-01-02 10 NaN -15.0
2023-01-03 24 NaN 14.0
2023-01-04 23 NaN -1.0
2023-01-05 17 NaN -6.0
... .. ... ...
2023-06-26 22 21.142857 -5.0
2023-06-27 29 22.714286 7.0
2023-06-28 6 20.142857 -23.0
2023-06-29 6 19.571429 0.0
2023-06-30 24 18.285714 18.0[181 rows x 3 columns]处理缺失值: 温度 7天滚动平均 温度差分
日期
2023-01-01 25.0 NaN NaN
2023-01-02 10.0 NaN -15.0
2023-01-03 24.0 NaN 14.0
2023-01-04 23.0 NaN -1.0
2023-01-05 17.0 NaN -6.0
... ... ... ...
2023-06-26 22.0 21.142857 -5.0
2023-06-27 29.0 22.714286 7.0
2023-06-28 6.0 20.142857 -23.0
2023-06-29 6.0 19.571429 0.0
2023-06-30 24.0 18.285714 18.0[181 rows x 3 columns]月度数据:温度 7天滚动平均 温度差分
日期
1 14.290323 12.725714 -0.233333
2 22.821429 20.678571 -0.321429
3 18.419355 18.801843 0.322581
4 18.333333 17.666667 0.366667
5 17.193548 18.064516 -0.967742
6 20.500000 20.085714 0.800000季度数据:温度 7天滚动平均 温度差分
日期
2023-03-31 18.366667 17.619048 -0.067416
2023-06-30 18.659341 18.599686 0.054945时间区间和偏移量操作:温度 7天滚动平均 温度差分
日期
2023-01-02 25.0 NaN NaN
2023-01-03 10.0 NaN -15.0
2023-01-04 24.0 NaN 14.0
2023-01-05 23.0 NaN -1.0
2023-01-06 17.0 NaN -6.0
... ... ... ...
2023-06-27 22.0 21.142857 -5.0
2023-06-28 29.0 22.714286 7.0
2023-06-29 6.0 20.142857 -23.0
2023-06-30 6.0 19.571429 0.0
2023-07-01 24.0 18.285714 18.0[181 rows x 3 columns]