如何在Pandas中使用时间序列?
时间序列数据主要用于处理随时间变化的数据。处理这些数据在时间序列数据的分析中起着非常重要的作用。Pandas是Python中一个流行的数据操作和分析库,它提供了强大的功能来处理时间序列数据。在本文中,我们将通过示例和解释来了解如何在Pandas中有效地利用时间序列。
利用时间序列数据的方法
在下面的方法中,我们将使用从Kaggle获取的Electric_production时间序列数据集。你可以从此处下载数据集。
导入和操作时间序列数据
在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 # Load time series data from a CSV file data = pd.read_csv('Electric_Production.csv') # 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() # Select data for a specific date range subset_1 = data['2017-01-01':'2017-10-30'] print(subset_1) # Select data for a specific month subset_2 = data[data.index.month == 3] print(subset_2) # Select data for a specific year 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简化了整个时间序列分析工作流程。通过利用本文中讨论的功能,分析师和数据科学家可以获得有价值的见解,并根据基于时间的数据做出明智的决策。