如何将 Pandas 索引转换为月份名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32699950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:54:45  来源:igfitidea点击:

How to convert Pandas Index to month name

pythonpandas

提问by I am not George

I am trying to change my Date index in the following time series to the month name.

我正在尝试将以下时间序列中的日期索引更改为月份名称。

website = dfFinal.groupby(['Date','Website'])

websiteGroup = website['Visits'].aggregate(np.sum).unstack()

Website              A        B          C
Date                                      
2015-01-01       18185   805769        NaN
2015-02-01       73236   944458        NaN
2015-03-01      101737  1003966        NaN
2015-04-01      101018   861229        NaN
2015-05-01       77724   845223        NaN
2015-06-01      111503   966043        NaN
2015-07-01      115413   937184        NaN
2015-08-01      115215   890457       1649

for example I want it to look like this:

例如,我希望它看起来像这样:

    Website           A        B          C
    Date                                      
    January       18185   805769        NaN
    February      73236   944458        NaN
    March        101737  1003966        NaN
    April        101018   861229        NaN
    May           77724   845223        NaN
    June         111503   966043        NaN
    July         115413   937184        NaN
    August       115215   890457       1649

I want to be able to this so my plot ticks will be the month name instead of the datetime.

我希望能够做到这一点,所以我的情节刻度将是月份名称而不是日期时间。

Thanks

谢谢

edit//

编辑//

same scenario but solution does not work on it:

相同的场景,但解决方案对它不起作用:

systemType = dfFinal.groupby(['Date','Website','Type'])
systemGroup = systemType['Visits'].aggregate(np.sum)
systemGroup = systemGroup.groupby(level=[0,1]).apply(lambda x: 100*x/float(x.sum())).unstack()

Type                      Other  Windows Mobile  Windows PC  
Date       Website                                           
2015-01-01 A           0.637888        0.005499   48.814957  
           B           0.686549        0.016506   54.176073  
2015-02-01 A           0.742804        0.020482   49.811568  
           B           0.651802        0.014506   57.014288  
2015-03-01 A           0.668390        0.014744   50.087972  
           B           0.573924        0.015937   59.906013  
2015-04-01 A           0.662258        0.015839   49.310024  
           B           0.583933        0.013469   59.490449  
2015-05-01 A           0.666461        0.020586   48.522979  
           B           0.577954        0.017983   58.838200  


systemGroup = systemGroup.rename(index=lambda x: x.strftime('%B'))

gives me an error

给我一个错误

AttributeError: 'str' object has no attribute 'strftime'

采纳答案by TomAugspurger

If you have a DatetimeIndex, you can use

如果您有 DatetimeIndex,则可以使用

websiteGroup.rename(index=lambda x: x.strftime('%B'))

.renamecan take a function, and we'll use the '%B'code for the full month name.

.rename可以接受一个函数,我们将使用'%B'完整月份名称的代码。

回答by jezrael

Use DatetimeIndex.strftime:

使用DatetimeIndex.strftime

websiteGroup.index = websiteGroup.index.strftime('%B')
print (websiteGroup)
               A        B       C
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0


df = websiteGroup.set_index(websiteGroup.index.strftime('%b'))
print (df)
          A        B       C
Jan   18185   805769     NaN
Feb   73236   944458     NaN
Mar  101737  1003966     NaN
Apr  101018   861229     NaN
May   77724   845223     NaN
Jun  111503   966043     NaN
Jul  115413   937184     NaN
Aug  115215   890457  1649.0

Also for assign new values in index is possible use set_index:

也可以用于在索引中分配新值set_index

df = websiteGroup.set_index(websiteGroup.index.strftime('%B'))
print (df)
               A        B       C
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0

EDIT:

编辑:

For versions pandas 0.23.0is possible use DatetimeIndex.month_name:

对于版本pandas 0.23.0可以使用DatetimeIndex.month_name

websiteGroup.index = websiteGroup.index.month_name()
print (websiteGroup)
               A        B       C
Website                          
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0

回答by onlyphantom

Pandas version 0.23.0 and onwards (as of this writing, it's 0.24.2) provide a built-in method: .month_name. From its official documentation:

Pandas 0.23.0 及更高版本(在撰写本文时,它是 0.24.2)提供了一个内置方法:.month_name. 从它的官方文档

pandas.DatetimeIndex.month_namereturns the month names of the DateTimeIndex with specified locale.

pandas.DatetimeIndex.month_name返回具有指定语言环境的 DateTimeIndex 的月份名称。

Consider the following DataFrame:

考虑以下数据帧:

aapl.tail()
# returns:
Attributes  High    Low Open    Close   Volume  Adj Close
2019-03-27  189.76  186.55  188.75  188.47  29848400.0  188.47
2019-03-28  189.56  187.53  188.95  188.72  20780400.0  188.72
2019-03-29  190.08  188.54  189.83  189.95  23564000.0  189.95
2019-03-30  190.08  188.54  189.83  189.95  23564000.0  189.95
2019-03-31  190.08  188.54  189.83  189.95  23564000.0  189.95

The DataFrame has a DateTimeIndex, so we can apply .month_nameon the index like so:

DataFrame 有一个 DateTimeIndex,所以我们可以.month_name像这样应用索引:

aapl.index = aapl.index.month_name()
aapl.tail()
# returns:
Attributes  High    Low Open    Close   Volume  Adj Close
March   189.76  186.55  188.75  188.47  29848400.0  188.47
March   189.56  187.53  188.95  188.72  20780400.0  188.72
March   190.08  188.54  189.83  189.95  23564000.0  189.95
March   190.08  188.54  189.83  189.95  23564000.0  189.95
March   190.08  188.54  189.83  189.95  23564000.0  189.95

Prior to 0.23.0 you would use .month()and other answers referencing the use of .strftime('%B')is the way to go.

在 0.23.0 之前,您将使用.month()和其他参考使用的答案.strftime('%B')是要走的路。

回答by multivac

You can parse each date string with datetime.strptimeand print the month name with datetime.strftime('%B'):

您可以使用以下命令解析每个日期字符串datetime.strptime并打印月份名称datetime.strftime('%B')

>>> d = datetime.datetime.strptime('2015-01-01', '%Y-%m-%d')
>>> d.strftime('%B')
'January'