2017-09-20 10 views
1

利用できる入力の下に検討してください。numpyの日時への変換文字列だけ年の情報が

[['Fiscal data as of Dec 31 2016', '2016', '2015', '2014'], 
['Fiscal data as of Mar 31 2016', '2016', '2015', '2014']] 

私の所望の出力は次のようになります。

[[2016-12-31, 2015-12-31, 2014-12-31], 
[2016-03-31, 2015-03-31, 2014-12-31]] 

基本的に、私は各nestedlist内の要素を1-3変換したいです要素0の値に基づいてmonthの情報を持つdatetimeオブジェクトに変換します。listです。

私は手動で集中的な解決策を考えることができますが、私はこれを達成するための最も効率的な方法(スピードワイズ)を探しています。実際のデータにはこのような行が何千もあります。

答えて

1

あなたは、daysmonthsためextractを使用raddでrigthからeache年に追加し、to_datetimeに変換することができます:

L = [['Fiscal data as of Dec 31 2016', '2016', '2015', '2014'], 
['Fiscal data as of Mar 31 2016', '2016', '2015', '2014']] 

a = np.array(L) 
pat = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})' 
d = pd.Series(a[:, 0]).str.extract(pat, expand=True).apply('-'.join, 1).add('-') 
print (d) 
0 Dec-31- 
1 Mar-31- 
dtype: object 

L1 = pd.DataFrame(a[:, 1:]).radd(d, 0).apply(pd.to_datetime).values.astype('datetime64[D]') 
print (L1) 
[['2016-12-31' '2015-12-31' '2014-12-31'] 
['2016-03-31' '2015-03-31' '2014-03-31']] 

パフォーマンスがマッピングヶ月間の重要な使用dictionaryの場合:

d = {'Jan':'01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06', 
    'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12'} 

L2 = [] 
for l in L: 
    a = l[0].split()[-3:-1] 
    a = '-'.join([d[a[0]], a[1]]) 
    L2.append([x + '-' + a for x in l[1:]]) 

print (L2) 

[['2016-12-31', '2015-12-31', '2014-12-31'], 
['2016-03-31', '2015-03-31', '2014-03-31']] 

最後に必要な場合はnumpy array

print (np.array(L1)) 
[['2016-12-31' '2015-12-31' '2014-12-31'] 
['2016-03-31' '2015-03-31' '2014-03-31']] 

タイミング

L = [['Fiscal data as of Dec 31 2016', '2016', '2015', '2014'], 
['Fiscal data as of Mar 31 2016', '2016', '2015', '2014']] * 10000 


In [262]: %%timeit 
    ...: d = {'Jan':'01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06', 
    ...:  'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12'} 
    ...: 
    ...: L2 = [] 
    ...: for l in L: 
    ...:  a = l[0].split()[-3:-1] 
    ...:  a = '-'.join([d.get(a[0]), a[1]]) 
    ...:  L2.append([x + '-' + a for x in l[1:]]) 
    ...: 
10 loops, best of 3: 44.3 ms per loop 

In [263]: %%timeit 
    ...: out_list=[] 
    ...: for l in L: 
    ...:  l_date = datetime.strptime((" ").join(l[0].split()[-3:]), '%b %d %Y') 
    ...:  out_list.append([("-").join([str(l_year),str(l_date.month),str(l_date.day)]) 
    ...:    for l_year in l[-3:]]) 
    ...: 
1 loop, best of 3: 303 ms per loop 

In [264]: %%timeit 
    ...: a = np.array(L) 
    ...: pat = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})' 
    ...: d = pd.Series(a[:, 0]).str.extract(pat, expand=True).apply('-'.join, 1).add('-') 
    ...: L1 = pd.DataFrame(a[:, 1:]).radd(d, 0).apply(pd.to_datetime).values.astype('datetime64[D]') 
    ...: 
1 loop, best of 3: 7.46 s per loop 
+1

を作成し、私はあなたのnumpyのソリューションを好むが、それはまた、より直感的です。ありがとう –

0

これは、ネストされたリストとして、ご希望の出力

from datetime import datetime 

in_list = [['Fiscal data as of Dec 31 2016', '2016', '2015', '2014'], 
['Fiscal data as of Mar 31 2016', '2016', '2015', '2014']] 

out_list=[] 
for l in in_list: 
    l_date = datetime.strptime((" ").join(l[0].split()[-3:]), '%b %d %Y') 
    out_list.append([("-").join([str(l_year),str(l_date.month),str(l_date.day)]) 
      for l_year in l[-3:]]) 
関連する問題