python - Pandas 在 DatetimeIndex TypeError : object of type 'NoneType' has no len() 上合并

标签 python pandas

举一个非常简单的例子:

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()。如果我想在索引上合并这两个 DataFrame(相同的 DatetimeIndex),我是否缺少合并的工作原理?

我正在运行 Python 2.7.12、Pandas 0.18.1 和 Numpy 1.11.1

完整的回溯是:

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()

最佳答案

documentation它指出:“left_on”可以是“标签或列表,或类似数组”当您传递“True”时,会出现错误。 如果你只是省略“left_on”,它似乎工作得很好。

我是否误解了这个问题?

也许你确实想这样做:

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

这会导致

    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

关于python - Pandas 在 DatetimeIndex TypeError : object of type 'NoneType' has no len() 上合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40551492/

相关文章:

python - 为 scrapy 安装 python - 符号链接(symbolic link)和权限问题

python - 从不同的列中取绝对值的最大值并过滤掉 NaN Python

Python 3 - Mac OS X 10.6.x - 如何捕获麦克风输入原始样本数据?

python - 在 Python 中处理异常堆栈跟踪的正确方法

python - Pandas 将多索引的所有级别转换为另一种类型

r - 类似于 R 中的 Pandas Series.value_counts()?

python - 通过在 python 中对值进行分组来绘制条形图

python - 如何将日期列添加到Python中已有的时间列?

Python:在 pandas 中计数后对日期进行排序

python - 使用 pytz 将本地时间转换为 UTC 会添加 DST?