Pandas - 数据系列 - 类型错误:索引必须是 DatetimeIndex [英] Pandas - Data Series - TypeError: Index must be DatetimeIndex

查看:102
本文介绍了Pandas - 数据系列 - 类型错误:索引必须是 DatetimeIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 是一个数据系列,形状为 (10000000, ),dtypes 为 dtype(< M8[ns]).我想创建一个仅包含一小时 C 的数据系列.

C is a Data series with shape of (10000000, ) with dtypes of dtype(< M8[ns]). I want to create a dataseries which only contain one hour of C.

 c.between_time('22:00:00','23:00:00')

这是我得到的错误

 TypeError: Index must be DatetimeIndex

我该如何解决?

推荐答案

尝试通过初始化一个空数据框来创建一个数据框.
我在系列中创建了 3 个虚拟时间的样本

Try creating a dataframe by initializing an empty dataframe.
I created a sample of 3 dummy times in Series

import pandas as pd  
C = pd.Series(['22:00:00','22:30:00','23:00:00'])

df = pd.DataFrame()
df['C'] = C

#set index to the obervations after converting them to type datetimeIndex
df.index = pd.to_datetime(C)
print df

print df.between_time(df['C'][0],df['C'][2])

这篇关于Pandas - 数据系列 - 类型错误:索引必须是 DatetimeIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆