2017-04-03 42 views
2

我有一系列索引采用月份日格式。它不是正式的日期时间索引。此外,该系列包含了一年的每一天一个数据:将“非正式”月份日指数转换为日期时间指数

 Data_Value 
01-01   156 
01-02   139 
01-03   133 
01-04   106 
01-05   128 
01-06   189 

我的目标是让一个线图与“Data_Value”在y轴和十二个月的X-轴。但我想绘制365个数据,而不是将它们汇总到12个月。事情是这样的:

enter image description here

无论如何,我的第一乐章已经绘制365个数据忽略把几个月的名字在x轴的事实:

s = np.array(s) 
plt.figure() 
plt.plot(s, '-o') 

但在过去命令我得到了错误“ValueError:无法将字符串转换为浮点数:'12 -31'”。

是否有人如何将“非正式”的月日指数转换为省略年份的日期时间指数?或者任何其他解决方案来达到我绘制系列的最终目标?谢谢。

回答

0

您可以使用:

np.random.seed(100) 
rng = pd.date_range('2017-01-01', periods=365).strftime('%m-%d') 
df = pd.DataFrame({ 'Data_value': np.random.randint(1000, size=365)}, index=rng) 
#print (df) 
d = {'01':'Jan', '02':'Feb', '03':'Mar','04': 'Apr', '05':'May','06': 'Jun', 
    '07':'Jul', '08':'Aug','09': 'Sep','10': 'Oct', '11':'Nov','12': 'Dec'} 

#add _for match only first value of string to dict 
d = {'_' + k:v for k, v in d.items()} 

#add _ to index 
df.index = '_' + df.index 
#split values by - to MultiIndex 
df.index = df.index.str.split('-', expand=True) 

#reshape and replace NaN to 0 
df = df['Data_value'].unstack(fill_value=0) 
#rename index values by dict 
df = df.rename(index=d) 
print (df) 
     01 02 03 04 05 06 07 08 09 10 ... 22 23 24 \ 
Jan 520 792 835 871 855 79 944 906 350 948 ... 316 570 912 
Feb 900 415 897 141 757 723 612 4 603 955 ...  2 889 617 
Mar 181 283 824 238 369 926 944 303 679 877 ... 618 30 17 
Apr 693 846 0 13 185 460 362 131 582 643 ... 811 36 773 
May 852 95 626 749 631 76 801 314 102 938 ... 419 407 765 
Jun 677 870 122 628 186 295 619 734 819 286 ... 16 524 854 
Jul 138 776 473 712 414 908 658 349 887 604 ... 389 435 346 
Aug 385 14 883 289 148 168 536 477 442 796 ... 730 250 477 
Sep 82 998 401 906 653 593 885 793 194 655 ... 944 754 506 
Oct 144 819 182 183 83 502 356 554 957 760 ... 70 309 994 
Nov 674 131 870 139 305 797 804 861 451 922 ... 723 119 71 
Dec 781 304 466 544 294 296 497 693 93 398 ... 915 716 322 

     25 26 27 28 29 30 31 
Jan 507 649 93 86 386 667 876 
Feb 478 403 994 63 0 0 0 
Mar 53 68 946 488 347 475 979 
Apr 839 38 214 94 738 170 0 
May 521 944 496 789 409 438 262 
Jun 466 621 67 220 223 788 0 
Jul 34 451 862 974 694 77 212 
Aug 736 74 437 798 67 668 933 
Sep 693 657 705 298 861 172 0 
Oct 736 943 944 905 689 821 879 
Nov 829 93 498 804 123 554 0 
Dec 141 87 65 324 182 640 343 

[12 rows x 31 columns] 

最后DataFrame.plot

df.plot() 
+0

我需要月和日,而不仅仅是月。但我想要横轴上的月份名称。我编辑了我以前的帖子,添加了一个清晰的概念图。顺便说一句,忘掉两条线和阴影区域。我只是一个人绘制一条线。 –

+0

你能解释更多吗?我在索引,列中创建数月和数天的数据框。现在我不明白如何创建一条线。还有左轴'40,0,-30'是什么? – jezrael

+0

好吧,所以得到2个数据帧 - 一个为最小值,另一个为最大值。但是现在如何在没有聚合的情况下进行策划有很多值,看起来''天''信息是不必要的,就像这个[编辑](http://stackoverflow.com/revisions/43178855/6)。还是缺少什​​么? – jezrael

0

这是我做了什么,为jezrael建议:

一个随机年加入指数,将其转换为datetime指数:

s.index = '2014-' + s.index.astype(str) 
s.index = pd.to_datetime(s.index) 
days_s = s.index 
days_s = np.array(days_s) 

然后设置,绘制图:从%Y-%间

plt.figure() 
plt.plot(days_s, s, '-o') 

而改变x轴使用mdates.DateFormatter%d到%米格式:

import matplotlib.dates as mdates 
monthsFmt = mdates.DateFormatter('%m') 
plt.gca().xaxis.set_major_formatter(monthsFmt) 

最后,渲染情节:

plt.show() 

我不知道这个解决方案是否有点草率,但它的工作原理。