2016-11-11 2 views
0

는 다음과 같은 아주 간단한 예를 가지고 :팬더는 DatetimeIndex 형식 오류에 병합 : 유형의 객체를 '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) 

오류, 형식 오류 반환 타입의 객체를 'NoneType'더는 len (이 없습니다). 같은 DatetimeIndex 인 인덱스에서이 두 DataFrames를 병합하려면 병합이 어떻게 작동해야하는지 모르겠습니다.

내가 파이썬 2.7.12, 팬더 0.18.1 및 NumPy와를 실행하고 1.11.1

전체 역 추적은 "left_on"할 수 있습니다 다음 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

그것은한다고 "레이블 또는 목록 또는 배열과 같은" "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가 가장 좋습니다 –