📜  如何在 Pandas 中利用时间序列?

📅  最后修改于: 2022-05-13 01:55:29.822000             🧑  作者: Mango

如何在 Pandas 中利用时间序列?

Python的pandas库提供了一套标准的时间序列工具和数据算法。通过这种方式,我们可以有效地处理非常大的时间序列,并轻松地对不规则和固定频率的时间序列进行切片、聚合和重新采样。

时间序列数据是结构化数据的一种重要形式,用于金融、经济、生态等领域。任何在多个时间点上观察或测量的事物都形成时间序列。

  1. 时间戳:这些是特定的时刻
  2. 固定期间:这将表示例如 5 月 25 日或 1999 年全年。

DateTime 中的模块

  • date:该模块用于以年、月、日的格式存储日历。
  • time:该模块用于以小时、分钟、秒和微秒的格式获取和显示时间。
  • datetime:该模块用于存储日期和时间。
  • timedelta:该模块用于获取两个日期时间值之间的差异。

以下是描述如何在 Pandas 库中使用时间序列的各种示例:

示例 1:显示当前日期和时间。在这个程序中,我们将显示当前日期和时间。



Python3
# import datetime module 
# for getting date and time
from datetime import datetime
  
# command to display 
# current date and time
datetime.now()


Python3
# import module
from datetime import datetime
  
# display all attributes
a=datetime.now()
print(a.year)
print(a.day)
print(a.month)
print(a.hour)
print(a.minute)
print(a.second)
print(a.microsecond)
print(a.date)


Python3
# importing time delta module
from datetime import timedelta
  
# subtracting date from year 2027 to 2021
deltaresult = datetime(2027, 5, 7) - datetime(2021, 6, 24)
  
# display the result
print(deltaresult)
  
# to get days
print(deltaresult.days)
  
# to get seconds difference
print(deltaresult.seconds)


Python3
# importing pandas module
import pandas as pd
  
# using date_range function to generate 
# dates from january 1 2021 to 
# march 16 2021 as periods = 75
dates = pd.date_range('1/1/2021', periods=75)
  
print(dates)


Python3
# importing pandas module
import pandas as pd
# importing numpy module for generating values
import numpy as np
  
# using date_range function to generate dates
# from january 1 2021 to march 16 2021 as periods = 75
dates = pd.date_range('1/1/2021', periods=75)
  
# giving values to dates
results = pd.Series(np.arange(75), index=dates)
  
# print results
print(results)
  
# converting to data frame
print(pd.DataFrame(results))


Python3
# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers 
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021',
                               '2/4/2020',
                               '1/3/2021',
                               '4/12/2017'],
                     'customers': [100, 30, 56, 56]})
  
# display original data
data
  
# converting dates column to date 
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# data after conversion
data


Python3
# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers 
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021', '2/4/2020', 
                               '1/3/2021', '4/12/2017',
                               '1/2/2021', '2/4/2020', 
                               '1/3/2021'], 
                     'customers': [100, 30, 56, 
                                   56, 23, 45, 67]})
# display original data
data
  
# converting dates column to date 
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# after conversion
data
  
# finding unique time series data
print(data['dates'].nunique())
  
# counting each series data
data['dates'].value_counts()


Python3
# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021', '2/4/2020',
                               '1/3/2021', '4/12/2017',
                               '1/2/2021', '2/4/2020',
                               '1/3/2021'],
                     'customers': [100, 30, 56,
                                   56, 23, 45, 67]})
# display original data
data
  
# converting dates column to date
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# depict visualization
data['dates'].hist(figsize=(10, 5), color="green")


输出:

示例 2:从模块中单独显示小时、分钟、秒、月、年、日的程序。

蟒蛇3

# import module
from datetime import datetime
  
# display all attributes
a=datetime.now()
print(a.year)
print(a.day)
print(a.month)
print(a.hour)
print(a.minute)
print(a.second)
print(a.microsecond)
print(a.date)

输出:

示例 3:两个日期之间的差异。我们可以使用 timedelta 模块获得小时、天和分钟的差异。



蟒蛇3

# importing time delta module
from datetime import timedelta
  
# subtracting date from year 2027 to 2021
deltaresult = datetime(2027, 5, 7) - datetime(2021, 6, 24)
  
# display the result
print(deltaresult)
  
# to get days
print(deltaresult.days)
  
# to get seconds difference
print(deltaresult.seconds)

输出:

如果我们要生成时间序列数据, Python将支持date_range模块。这将在给定频率内生成日期。它在 pandas 模块中可用。

示例 4:在此程序中,我们可以从 2021 年 1 月 1 日开始,并使用 date_range 方法显示截至 3 月的日期。

蟒蛇3

# importing pandas module
import pandas as pd
  
# using date_range function to generate 
# dates from january 1 2021 to 
# march 16 2021 as periods = 75
dates = pd.date_range('1/1/2021', periods=75)
  
print(dates)

输出:



通过使用时间序列作为索引生成对应日期的值。

示例 5:在此程序中,我们通过将日期设置为每个值的索引来为日期赋值。

蟒蛇3

# importing pandas module
import pandas as pd
# importing numpy module for generating values
import numpy as np
  
# using date_range function to generate dates
# from january 1 2021 to march 16 2021 as periods = 75
dates = pd.date_range('1/1/2021', periods=75)
  
# giving values to dates
results = pd.Series(np.arange(75), index=dates)
  
# print results
print(results)
  
# converting to data frame
print(pd.DataFrame(results))

输出:

我们可以使用以下方法将数据字符串列转换为日期时间类型。

句法:

示例 6:在这个程序中,我们将字符串数据转换为日期时间类型。

蟒蛇3



# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers 
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021',
                               '2/4/2020',
                               '1/3/2021',
                               '4/12/2017'],
                     'customers': [100, 30, 56, 56]})
  
# display original data
data
  
# converting dates column to date 
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# data after conversion
data

输出:

例7:在这个程序中,我们将一些时间序列数据作为索引,对其进行转换并验证它们是否相等。

蟒蛇3

# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers 
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021', '2/4/2020', 
                               '1/3/2021', '4/12/2017',
                               '1/2/2021', '2/4/2020', 
                               '1/3/2021'], 
                     'customers': [100, 30, 56, 
                                   56, 23, 45, 67]})
# display original data
data
  
# converting dates column to date 
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# after conversion
data
  
# finding unique time series data
print(data['dates'].nunique())
  
# counting each series data
data['dates'].value_counts()

输出:

示例 8:显示来自具有 DateTime 对象的数据帧的直方图的程序。

蟒蛇3

# importing pandas module for data frame
import pandas as pd
  
# creating data frame for different customers
# in a shop with respect to dates
data = pd.DataFrame({'dates': ['1/2/2021', '2/4/2020',
                               '1/3/2021', '4/12/2017',
                               '1/2/2021', '2/4/2020',
                               '1/3/2021'],
                     'customers': [100, 30, 56,
                                   56, 23, 45, 67]})
# display original data
data
  
# converting dates column to date
# using pandas.to_datetime function
data['dates'] = pd.to_datetime(data['dates'])
  
# depict visualization
data['dates'].hist(figsize=(10, 5), color="green")

输出: