2017-04-03 44 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() 

我不知道這個解決方案是否有點草率,但它的工作原理。