无法通过Datetime与pd.merge合并DataFrames

时间:2019-04-13 09:40:29

标签: python pandas dataframe

我有2个数据框需要合并。两者具有相同的日期时间。我需要比较日期时间,并按日期时间将DF A的值添加到相应的DF B行中。让我们看一下数据框:

df [“日期”]:

2013    2016-06-06
2174    2016-06-07
1432    2016-06-08
2035    2016-06-09
278     2016-06-10
2250    2016-06-11
908     2016-06-12
702     2016-06-13
1982    2016-06-14
630     2016-06-15
Name: Date, Length: 81, dtype: object

stocks [“ Date”]

47   2016-06-02
48   2016-06-03
49   2016-06-06
50   2016-06-07
51   2016-06-08
52   2016-06-09
53   2016-06-10
54   2016-06-13
55   2016-06-14
56   2016-06-15
Name: Date, dtype: datetime64[ns]

当我尝试通过以下方式合并数据框时:

df.merge(stocks, left_on='Date', right_on='Date')

我收到以下错误:

Traceback (most recent call last):
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-72-f2ca0c92995e>", line 1, in <module>
    df.merge(stocks, left_on='Date', right_on='Date')
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/pandas/core/frame.py", line 6379, in merge
    copy=copy, indicator=indicator, validate=validate)
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/pandas/core/reshape/merge.py", line 60, in merge
    validate=validate)
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/pandas/core/reshape/merge.py", line 554, in __init__
    self._maybe_coerce_merge_keys()
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/pandas/core/reshape/merge.py", line 984, in _maybe_coerce_merge_keys
    raise ValueError(msg)
ValueError: You are trying to merge on object and datetime64[ns] columns. If you wish to proceed you should use pd.concat

0 个答案:

没有答案