Pandas 如何利用时间序列
时间序列数据主要用于处理随时间变化的数据。处理这些数据在时间序列数据的数据分析中起着非常重要的作用。Pandas是Python中一个流行的数据处理和分析库,提供了强大的功能来处理时间序列数据。在本文中,我们将通过示例和解释来了解如何有效地利用Pandas中的时间序列。
利用时间序列数据的方式
在下面的方法中,我们将使用从Kaggle获取的Electric_ptoduction时间序列数据集。您可以从这里下载数据集。
导入和操作时间序列数据
在Pandas中处理时间序列数据时,我们首先需要导入必要的库并将数据加载到DataFrame中。Pandas提供了从不同来源(包括CSV文件、数据库和Web API)读取时间序列数据的各种方法。在数据加载后,Pandas提供了强大的工具来操作、清理和预处理时间序列数据。
import pandas as pd
# Load time series data from a CSV file
data = pd.read_csv('Electric_Production.csv')
# Display the first few rows of the DataFrame
print(data.head())
# Set the 'timestamp' column as the index
data['DATE'] = pd.to_datetime(data['DATE'])
data.set_index('DATE', inplace=True)
# Resample the data to a daily frequency
daily_data = data.resample('D').mean()
输出
DATE IPG2211A2N
0 1/1/1985 72.5052
1 2/1/1985 70.6720
2 3/1/1985 62.4502
3 4/1/1985 57.4714
4 5/1/1985 55.3151
索引和切片时间序列数据
Pandas包含多种索引和切片方法,用于从时间序列数据中提取特定的时间段或观测值。Pandas中的DateTimeIndex可以基于时间进行直观的索引和选择。
import pandas as pd
# 从CSV文件加载时间序列数据
data = pd.read_csv('Electric_Production.csv')
# 将 'timestamp' 列设置为索引
data['DATE'] = pd.to_datetime(data['DATE'])
data.set_index('DATE', inplace=True)
# 将数据重新采样到每天的频率
daily_data = data.resample('D').mean()
# 选择特定日期范围的数据
subset_1 = data['2017-01-01':'2017-10-30']
print(subset_1)
# 选择特定月份的数据
subset_2 = data[data.index.month == 3]
print(subset_2)
# 选择特定年份的数据
subset_3 = data[data.index.year == 2016]
print(subset_3)
输出
IPG2211A2N
DATE
2017-01-01 114.8505
2017-02-01 99.4901
2017-03-01 101.0396
2017-04-01 88.3530
2017-05-01 92.0805
2017-06-01 102.1532
2017-07-01 112.1538
2017-08-01 108.9312
2017-09-01 98.6154
2017-10-01 93.6137
IPG2211A2N
DATE
1985-03-01 62.4502
1986-03-01 62.2221
1987-03-01 65.6100
1988-03-01 70.2928
1989-03-01 73.3523
1990-03-01 73.1964
1991-03-01 73.3650
1992-03-01 74.5275
1993-03-01 79.4747
1994-03-01 79.2456
1995-03-01 81.2661
1996-03-01 86.9356
1997-03-01 83.0125
1998-03-01 86.5549
1999-03-01 90.7381
2000-03-01 88.0927
2001-03-01 92.8283
2002-03-01 93.2556
2003-03-01 94.5532
2004-03-01 95.4029
2005-03-01 98.9565
2006-03-01 98.4017
2007-03-01 99.1925
2008-03-01 100.4386
2009-03-01 97.8529
2010-03-01 98.2672
2011-03-01 99.1028
2012-03-01 93.5772
2013-03-01 102.9948
2014-03-01 104.7631
2015-03-01 104.4706
2016-03-01 95.3548
2017-03-01 101.0396
IPG2211A2N
DATE
2016-01-01 117.0837
2016-02-01 106.6688
2016-03-01 95.3548
2016-04-01 89.3254
2016-05-01 90.7369
2016-06-01 104.0375
2016-07-01 114.5397
2016-08-01 115.5159
2016-09-01 102.7637
2016-10-01 91.4867
2016-11-01 92.8900
2016-12-01 112.7694
处理缺失数据
时间序列数据经常包含缺失值,这可能会妨碍分析和建模。Pandas提供了几种处理缺失数据的方法,例如插值、向前填充或向后填充。这些方法有助于确保时间序列的连续性。
import pandas as pd
# Load time series data from a CSV file
data = pd.read_csv('Electric_Production.csv')
# Display the first few rows of the DataFrame
# print(data.head())
# Set the 'timestamp' column as the index
data['DATE'] = pd.to_datetime(data['DATE'])
data.set_index('DATE', inplace=True)
# Resample the data to a daily frequency
daily_data = data.resample('D').mean()
## Interpolate missing values
data['value'] = data['value'].interpolate()
print(data.head())
# Forward-fill missing values
data['value'] = data['value'].ffill()
print(data.head())
# Backward-fill missing values
data['value'] = data['value'].bfill()
print(data.head())
输出
value
DATE
1985-01-01 72.5052
1985-02-01 70.6720
1985-03-01 64.0717
1985-04-01 57.4714
1985-05-01 55.3151
value
DATE
1985-01-01 72.5052
1985-02-01 70.6720
1985-03-01 64.0717
1985-04-01 57.4714
1985-05-01 55.3151
value
DATE
1985-01-01 72.5052
1985-02-01 70.6720
1985-03-01 64.0717
1985-04-01 57.4714
1985-05-01 55.3151
重新采样和频率转换
重新采样涉及改变时间序列数据的频率。Pandas提供了上采样(增加频率)和下采样(减少频率)时间序列数据的方法。这允许在不同的时间间隔上对数据进行聚合或插值处理。
import pandas as pd
# Load time series data from a CSV file
data = pd.read_csv('Electric_Production.csv')
# Display the first few rows of the DataFrame
# print(data.head())
# Set the 'timestamp' column as the index
data['DATE'] = pd.to_datetime(data['DATE'])
data.set_index('DATE', inplace=True)
# Resample the data to a daily frequency
daily_data = data.resample('D').mean()
print(daily_data.head())
# Resample the data to a weekly frequency, taking the mean value
weekly_data = data.resample('W').mean()
print(weekly_data.head())
# Resample the data to a monthly frequency, taking the sum value
monthly_data = data.resample('M').sum()
print(weekly_data.head())
输出
value
DATE
1985-01-01 72.5052
1985-01-02 NaN
1985-01-03 NaN
1985-01-04 NaN
1985-01-05 NaN
value
DATE
1985-01-06 72.5052
1985-01-13 NaN
1985-01-20 NaN
1985-01-27 NaN
1985-02-03 70.6720
value
DATE
1985-01-06 72.5052
1985-01-13 NaN
1985-01-20 NaN
1985-01-27 NaN
1985-02-03 70.6720
绘制和可视化时间序列数据
Pandas与Matplotlib集成,后者是一个流行的数据可视化库,使得创建洞察力的图形和可视化时间序列数据变得容易。可视化可以帮助理解数据中的趋势、模式和异常。
import pandas as pd
import matplotlib.pyplot as plt
# Load time series data from a CSV file
data = pd.read_csv('Electric_Production.csv')
# Display the first few rows of the DataFrame
# print(data.head())
# Set the 'timestamp' column as the index
data['DATE'] = pd.to_datetime(data['DATE'])
data.set_index('DATE', inplace=True)
# Plot the time series data
data.plot()
plt.title('Time Series Data')
plt.xlabel('Date')
plt.ylabel('Value')
plt.show()
输出
结论
在本文中,我们讨论了如何使用pandas功能来处理时间序列数据。从导入和预处理数据到高级分析和可视化,Pandas简化了整个时间序列分析的工作流程。通过利用本文讨论的功能,分析师和数据科学家可以从基于时间的数据中获取有价值的见解,并作出明智的决策。