📜  Python Pandas – 将 PeriodIndex 对象转换为 Timestamp 并设置频率

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

Python Pandas – 将 PeriodIndex 对象转换为 Timestamp 并设置频率

在本文中,我们将讨论如何将周期索引对象转换为时间戳并在Python编程语言中设置频率。

pandas PeriodIndex.to_timestamp() 方法用于将 PeriodIndex 对象转换为 Timestamp 并设置频率。频率可以使用方法的'freq'参数设置。

示例 1:

熊猫包已导入。使用 pd.PeriodIndex()函数创建一个周期索引对象,其中我们传入一个 DateTime 值数组,频率指定为“年”。周期索引对象将具有 YearEnd 类型的频率。 PeriodIndex 对象通过使用 pd.to_timestamp() 方法转换为时间戳对象。

Python3
# import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents year.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'],
                             freq="Y")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp())


Python3
#import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents month.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'], 
                             freq="M")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='M'))


Python3
# import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents Day.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'],
                             freq="D")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='D'))


输出:

输出

示例 2:

在此示例中,我们将字符串“M”作为频率,这为我们提供了“MonthEnd”类型的周期索引对象。我们还指定时间戳对象的频率为“M”。

Python3

#import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents month.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'], 
                             freq="M")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='M'))

输出:

输出

示例 3:

在此示例中,我们将字符串“D”作为频率,这为我们提供了“Day”类型的周期索引对象。我们还指定时间戳对象的频率为“D”。周期索引对象值与时间戳对象中的值完全匹配,因为我们将频率指定为“日”。

Python3

# import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents Day.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'],
                             freq="D")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='D'))

输出:

输出