Python 3.6.5 returns '<' not supported between instances of 'tuple' and 'str' 错误消息

Python 3.6.5 returns '<' not supported between instances of 'tuple' and 'str' error message

我正在尝试将数据集拆分为训练和测试部分。我正在努力解决一个结构性问题,因为数据的层次结构似乎是错误的,无法继续执行以下代码。

我尝试了以下方法:

import pandas as pd
data = pd.DataFrame(web.DataReader('SPY', data_source='morningstar')['Close'])
cutoff = '2015-1-1'
data = data[data.index < cutoff].dropna().copy()

正如 data.head() 所揭示的那样,data 实际上不是 pd.DataFrame,而是索引为 pd.MultiIndexpd.Series(同样由提示每个元素都是元组的错误)而不是 pd.DatetimeIndex.

你可以做的就是让

df = data.unstack(0)

由此,df[df.index < cutoff] 执行您正在尝试执行的过滤。