Python Pandas PeriodIndex.strftime

Python Pandas PeriodIndex.strftime

Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。

Pandas PeriodIndex.strftime()函数返回一个由date_format指定的格式化字符串数组,它支持与python标准库相同的字符串格式。

语法 : PeriodIndex.strftime(date_format)

参数 :
date_format :日期格式字符串(例如,”%Y-%m-%d”)。

返回 : ndarray of formatted strings

示例#1:使用PeriodIndex.strftime()函数以指定的date_format打印给定的PeriodIndex对象。

# importing pandas as pd
import pandas as pd
  
# Create the PeriodIndex object
pidx = pd.PeriodIndex(start = '2004-11-11 02:45:21 ',
               end = '2021-5-21 8:45:29', freq = 'Y')
  
# Print the PeriodIndex object
print(pidx)

输出 :

Python Pandas PeriodIndex.strftime

现在我们将使用PeriodIndex.strftime()函数,以(‘%b. %d, %Y是%A’)格式返回每个周期元素。

# return the PeriodIndex in specified format
pidx.strftime('% b. % d, % Y was a % A')

]

输出 :

Python Pandas PeriodIndex.strftime

正如我们在输出中看到的,PeriodIndex.strftime()函数返回了一个Index对象,其中包含了指定格式的给定PeriodIndex对象的每个元素。

示例#2:使用PeriodIndex.strftime()函数以指定的date_format打印给定的PeriodIndex对象。

# importing pandas as pd
import pandas as pd
  
# Create the PeriodIndex object
pidx = pd.PeriodIndex(start = '2016-10-12 11:12:02', 
            end = '2020-04-12 11:32:12', freq = 'Q')
  
# Print the PeriodIndex object
print(pidx)

输出 :

Python Pandas PeriodIndex.strftime

现在我们将使用PeriodIndex.strftime()函数,以(‘%b-%Y’)格式返回每个周期元素。

# return the PeriodIndex in specified format
pidx.strftime('% b-% Y')

]

输出 :

Python Pandas PeriodIndex.strftime

正如我们在输出中看到的,PeriodIndex.strftime()函数已经返回了一个Index对象,其中包含了指定格式的PeriodIndex对象的每个元素。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程