我有一个DataFrame,日期字段作为索引。以下将作为示例
import pandas as pd
import datetime
rand = np.random.RandomState(1)
index = date_range('2/1/2015', periods=8))
df = DataFrame(rand.randn(8, 1), index=index, columns=['A'])
print df
A
2015-02-01 1.624345
2015-02-02 -0.611756
2015-02-03 -0.528172
2015-02-04 -1.072969
2015-02-05 0.865408
2015-02-06 -2.301539
2015-02-07 1.744812
2015-02-08 -0.761207
我正在定义一个开始'并且'结束'像这样的变量:
start = datetime.datetime(2015,2,2)
end = datetime.datetime(2015,2,6)
...并像这样访问索引:
xStart = df.index.get_loc(start)
print xStart
xEnd = df.index.get_loc(end)
print xEnd
1
5
我想知道变量的值' start'并且'结束'存储在csv文件中,可以更新一次并由多个脚本访问。但格式导致我出现问题
我已经导入了这样的csv文件:
datefile = r'c:\testt\datefile.csv'
df2 = pd.read_csv(datefile, names=['date'])
print df2
date
0 2015,02,6
...并尝试了各种日期格式,在csv文件中以及导入到DataFrame之后,但每次都会收到TypeError消息。
有人可以建议我如何做这项工作吗?
编辑 - 添加追溯:
TypeError Traceback (most recent call last)
<ipython-input-105-f2f0b55d49f7> in <module>()
----> 1 xEnd = df.index.get_loc(end)
2 print xEnd
C:\Users\Bob\Anaconda\lib\site-packages\pandas\tseries\index.pyc in get_loc(self, key)
1307
1308 try:
-> 1309 return Index.get_loc(self, key)
1310 except (KeyError, ValueError):
1311 try:
C:\Users\Bob\Anaconda\lib\site-packages\pandas\core\index.pyc in get_loc(self, key)
1154 loc : int if unique index, possibly slice or mask if not
1155 """
-> 1156 return self._engine.get_loc(_values_from_object(key))
1157
1158 def get_value(self, series, key):
C:\Users\Bob\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.DatetimeEngine.get_loc (pandas\index.c:9475)()
C:\Users\Bob\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.DatetimeEngine.get_loc (pandas\index.c:8954)()
TypeError: