python 我想用日期时间设置索引数据帧

标签 python list date pandas datetimeindex

poorList = [datetime.date(2016, 5, 2), 
            datetime.date(2016, 8, 26), 
            datetime.date(2016, 6, 9), 
            datetime.date(2016, 3, 4)]

dateForm.set_index(poorList)

然后是错误:

File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4066) File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:3930) File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12408) File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12359) KeyError: datetime.date(2016, 5, 2)

请告诉我怎么做?

最佳答案

DataFrame.set_index() 需要一个列名或列列表作为参数,所以你应该这样做:

dateForm['date'] = poorList
dateForm.set_index('date', inplace=True)

关于python 我想用日期时间设置索引数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443206/

相关文章:

python - 如何使用join方法和sort方法

python - 使用空值处理解析 pandas.read_csv 中的日期?

html - 垂直居中对齐列表中的图像和文本

java - 由于静态问题,时间不会以动态形式返回

javascript - Javascript 中包含非数字字符的字符串的数字数组

Python 3.2.3、easy_install、Mac OS X

python - 模拟socket.recv()

Python - 存储在变量中的字符串显示为元组

python - 拆分文件中的列表

r - 如何在处理日期时手动向 ggplot2 添加中断?