2016-11-11 3 views
0

は、以下の非常に簡単な例を取る:パンダはDatetimeIndexはTypeErrorにマージ:型のオブジェクトを 'NoneType'(何LENを有していない)

import pandas as pd 
import numpy as np 
import datetime 

base = datetime.datetime(2016, 10, 1) 
date_list = [base - datetime.timedelta(days=x) for x in range(0, 100)] 

df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'), index = date_list) 
df2 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'), index = date_list) 

pd.merge(df1, df2, how = 'outer', left_on = True) 

エラー、例外TypeError返し:タイプのオブジェクトを 'NoneType'(何LENを有していません)。これらの2つのDataFramesを同じDatetimeIndexであるインデックスにマージしたい場合、マージがどのように機能するのか分かりませんか?

私は完全なトレースバックがあるのPython 2.7.12、パンダ0.18.1、およびnumpyの1.11.1

を実行しています:documentation

TypeError         Traceback (most recent call last) 
<ipython-input-1-3174c0ff542d> in <module>() 
     9 df2 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'), index = date_list) 
    10 
---> 11 pd.merge(df1, df2, how = 'outer', left_on = True) 

/Users/user/anaconda/lib/python2.7/site-packages/pandas/tools/merge.pyc in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator) 
    36       right_on=right_on, left_index=left_index, 
    37       right_index=right_index, sort=sort, suffixes=suffixes, 
---> 38       copy=copy, indicator=indicator) 
    39  return op.get_result() 
    40 if __debug__: 

/Users/user/anaconda/lib/python2.7/site-packages/pandas/tools/merge.pyc in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator) 
    208   (self.left_join_keys, 
    209   self.right_join_keys, 
--> 210   self.join_names) = self._get_merge_keys() 
    211 
    212  def get_result(self): 

/Users/user/anaconda/lib/python2.7/site-packages/pandas/tools/merge.pyc in _get_merge_keys(self) 
    405   left_keys, right_keys 
    406   """ 
--> 407   self._validate_specification() 
    408 
    409   left_keys = [] 

/Users/user/anaconda/lib/python2.7/site-packages/pandas/tools/merge.pyc in _validate_specification(self) 
    521          'of levels in the index of "left"') 
    522     self.left_on = [None] * n 
--> 523   if len(self.right_on) != len(self.left_on): 
    524    raise ValueError("len(right_on) must equal len(left_on)") 
    525 

TypeError: object of type 'NoneType' has no len() 

答えて

1

それはと述べている: "left_onは" することができ"ラベルまたはリスト、または配列のような"あなたが "True"を渡すとエラーが発生します。 "left_on"を省略するとうまくいくようです。

質問を誤解しましたか?

Mabyeあなたが実際にこれを行うにはしたくなかった:

pd.merge(df1, df2, how = 'outer', left_index = True, right_index=True) 

は、このような状況の下で、うまく上のジョインとしてjoin` `に固執する方が良い、また

A_x B_x C_x D_x A_y B_y C_y D_y 
2016-10-01 99 9 89 27 2 10 63 44 
2016-09-30 42 74 58 87 33 56 83 72 
2016-09-29 89 41 89 94 75 66 74 17 
2016-09-28 53 42 4 83 84 48 2 36 
2016-09-27 81 97 1 14 86 27 49 53 
+1

につながることになります重複するインデックス - 'df1.join(df2、lsuffix = '_ l'、rsuffix = '_ r') ' –

+0

ありがとう!私は完全にマージのためのドキュメントを誤読しているに違いありません。 –

+1

'join 'も、1000ループ、3ループ:468μs、%の間にマージ'%timeit df1.join(df2、lsuffix =' _ l '、rsuffix =' _ r ' timeit pd.merge(df1、df2、left_index = True、right_index = True) 'は1000ループを返します.3ループのうち485μs –

関連する問題