我的DataFrame(TradeData)的索引是字符串格式:
In [30]: TradeData.index
Out[30]: Index(['09/30/2013 : 04:14 PM', '09/30/2013 : 03:53 PM', ... ], dtype=object)
我希望它能在Datetime。但转换似乎不起作用:
In [31]: TradeDataIdxd = pd.to_datetime(TradeData.index, format="%m/%d/%Y : %I:%M %p")
Traceback (most recent call last):
File "<ipython-input-31-1191c22cd132>", line 1, in <module>
TradeDataIdxd = pd.to_datetime(TradeData.index, format="%m/%d/%Y : %I:%M %p")
File "C:\WinPython-64bit-3.3.2.3\python-3.3.2.amd64\lib\site-packages\pandas\tseries\tools.py", line 128, in to_datetime
return _convert_listlike(arg, box=box)
File "C:\WinPython-64bit-3.3.2.3\python-3.3.2.amd64\lib\site-packages\pandas\tseries\tools.py", line 104, in _convert_listlike
result = tslib.array_strptime(arg, format)
File "tslib.pyx", line 1137, in pandas.tslib.array_strptime (pandas\tslib.c:18543)
KeyError: 'p'
TradeData.index的所有元素都不是'p'。任何想法可能是什么问题?提前谢谢。
答案 0 :(得分:3)
您可以通过重置索引,通过map / lambda / strptime操作系列,然后最终再次设置索引来绕过这个to_datetime问题。
In [1058]: TradeData.index
Out[1058]: Index([u'09/30/2013 : 04:14 PM', u'09/30/2013 : 03:53 PM', u'09/30/2013 : 03:53 PM'], dtype=object)
In [1059]: index_name = TradeData.index.name
In [1060]: TradeData = TradeData.reset_index()
In [1061]: TradeData[index_name] = TradeData[index_name].map(lambda x: datetime.strptime(x, "%m/%d/%Y
: %I:%M %p"))
In [1062]: TradeData = TradeData.set_index(index_name)
In [1063]: TradeData.index
Out[1063]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-09-30 16:14:00, ..., 2013-09-30 15:53:00]
Length: 3, Freq: None, Timezone: None
不太简洁,但效果相同。或者,将其打包成一个函数:
def df_index_to_datetime(df, datetime_format):
index_name = df.index.name
df = df.reset_index()
df[index_name] = df[index_name].map(lambda x: datetime.strptime(x, datetime_format))
df = df.set_index(index_name)
return df
答案 1 :(得分:0)
更简单的解决方案是修复字符串,使其与to_datetime
期望的内容相匹配...
from pandas import *
ix = Index(['09/30/2013 : 04:14 PM', '09/30/2013 : 03:53 PM'], dtype=object)
to_datetime(ix.to_series().str.replace(': ',''))
09/30/2013 : 04:14 PM 2013-09-30 16:14:00
09/30/2013 : 03:53 PM 2013-09-30 15:53:00
dtype: datetime64[ns]