应用这两个数据帧时数据帧位置错误

error in dataframe loc when applying these two dataframes

我正在尝试将这两个数据帧(data2 和交易)放在一起以使其看起来像这样 https://i.stack.imgur.com/pR8bW.png:

数据2:

            Close
2015-08-28  113.290001
2015-08-31  112.760002
2015-09-01  107.720001
2015-09-02  112.339996
2015-09-03  110.370003
2015-09-04  109.269997
2015-09-08  112.309998
2015-09-09  110.150002
2015-09-10  112.570000
2015-09-11  114.209999

交易:

               Trades
2015-08-28     3.0
2015-08-31     3.0
2015-09-01     3.0
2015-09-02     3.0
2015-09-03     2.0

代码:

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111, ylabel='Portfolio value in $')

data2["Close"].plot(ax=ax1, lw=2.)

ax1.plot(data2.loc[trades.Trades == 2.0].index, data2.total[trades.Trades == 2.0],
         '^', markersize=10, color='m')
ax1.plot(data2.loc[trades.Trades == 3.0].index, 
         data2.total[trades.Trades == 3.0],
         'v', markersize=10, color='k')

plt.show()

但这会产生以下错误:

---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-38-9cde686354a8> in <module>()
      7 data2["Close"].plot(ax=ax1, lw=2.)
      8 
----> 9 ax1.plot(data2.loc[trades.Trades == 2.0].index, data2.total[trades.Trades == 2.0],
     10          '^', markersize=10, color='m')
     11 ax1.plot(data2.loc[trades.Trades == 3.0].index, 

3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in check_bool_indexer(index, key)
   2316         if mask.any():
   2317             raise IndexingError(
-> 2318                 "Unalignable boolean Series provided as "
   2319                 "indexer (index of the boolean Series and of "
   2320                 "the indexed object do not match)."

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

两个数据帧的索引不同。我采用了为 data2 数据帧定义 masks 的方法,该数据帧基于 trades 数据帧中的值并且有效。

此外,您的示例代码引用了 total,但不存在。使用 Close

的更新
import pandas as pd
import io
import matplotlib.pyplot as plt

data2 = pd.read_csv(io.StringIO("""            Close
2015-08-28  113.290001
2015-08-31  112.760002
2015-09-01  107.720001
2015-09-02  112.339996
2015-09-03  110.370003
2015-09-04  109.269997
2015-09-08  112.309998
2015-09-09  110.150002
2015-09-10  112.570000
2015-09-11  114.209999"""), sep="\s+")

trades = pd.read_csv(io.StringIO("""               Trades
2015-08-28     3.0
2015-08-31     3.0
2015-09-01     3.0
2015-09-02     3.0
2015-09-03     2.0"""), sep="\s+")

# make sure it's dates
data2 = data2.reset_index().assign(index=lambda x: pd.to_datetime(x["index"])).set_index("index")
trades = trades.reset_index().assign(index=lambda x: pd.to_datetime(x["index"])).set_index("index")

fig = plt.figure()
ax1 = fig.add_subplot(111, ylabel='Portfolio value in $')

data2["Close"].plot(ax=ax1, lw=2.)

mask2 = data2.index.isin((trades.Trades == 2.0).index)
mask3 = data2.index.isin((trades.Trades == 3.0).index)

ax1.plot(data2.loc[mask2].index, data2.Close[mask2],
         '^', markersize=10, color='m')
ax1.plot(data2.loc[mask3].index, 
         data2.Close[mask3],
         'v', markersize=10, color='k')


plt.show()

输出