如何将日期时间字段从数字转换为完整的月份名称

                High    Low Open    Close   Volume  Adj Close   year    pct_day
Date    month   week    day                             
1989-01-03  1   1   3   277.720001  273.809998  277.720001  275.309998  128500000   275.309998  1989    NaN
1989-01-04  1   1   4   279.750000  275.309998  275.309998  279.429993  149700000   279.429993  1989    0.014965
1989-01-05  1   1   5   281.510010  279.429993  279.429993  280.010010  174040000   280.010010  1989    0.002076
1989-01-06  1   1   6   282.059998  280.010010  280.010010  280.670013  161330000   280.670013  1989    0.002357
1989-01-09  1   2   9   281.890015  280.320007  280.670013  280.980011  163180000   280.980011  1989    0.001104
... ... ... ... ... ... ... ... ... ... ... ...
2006-12-22  12  51  22  1418.819946 1410.280029 1418.099976 1410.760010 1647590000  1410.760010 2006    -0.005316
2006-12-26  12  52  26  1417.910034 1410.449951 1410.750000 1416.900024 1310310000  1416.900024 2006    0.004352
2006-12-27  12  52  27  1427.719971 1416.630005 1416.630005 1426.839966 1667370000  1426.839966 2006    0.007015
2006-12-28  12  52  28  1427.260010 1422.050049 1426.770020 1424.729980 1508570000  1424.729980 2006    -0.001479
2006-12-29  12  52  29  1427.000000 1416.839966 1424.709961 1418.300049 1678200000  1418.300049 2006    -0.004513
4539 rows × 8 columns

我使用data['month'] = data.index.month创建并将索引设置为月份号。是否可以重新设置格式,以便我可以更改数字Jan而不是March的格式?日期仍然需要在datetime字段中。

我尝试了data['month'] = data.index.month.format(month,'%B'),但遇到此错误:

NameError: name 'month' is not defined

我想将名称更改为df.plot(y='pct_day')

如何将日期时间字段从数字转换为完整的月份名称

appyle 回答:如何将日期时间字段从数字转换为完整的月份名称

from datetime import datetime
dateobj = datetime.strptime('2006-11-22','%Y-%m-%d')

print(dateobj.strftime('%B'))
print(dateobj.strftime('%b'))

这将打印

November
Nov
本文链接:https://www.f2er.com/3161881.html

大家都在问