2013-10-28 15 views
5

indeks mojego DataFrame (TradeData) jest w formacie ciągu:KeyError w pand to_datetime użyciu niestandardowego formatu

In [30]: TradeData.index 
Out[30]: Index(['09/30/2013 : 04:14 PM', '09/30/2013 : 03:53 PM', ... ], dtype=object) 

I chciałbym go mieć w DateTime. Ale konwersja nie działa:

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' 

Żaden z elementów TradeData.index nie jest "p". Jakieś pomysły, co może być ważne? Z góry dziękuję.

+0

Yep .... nie zaimplementowany w C -code .... pls wydał problem dla tego – Jeff

+0

@Jeff done - https://github.com/pydata/pandas/issues/5361. Widzę kod "p", spróbuję to sprawdzić później. Może to będzie mój pierwszy wkład w pandy :) –

+0

dzięki ... to jest zaimplementowane w tslib.pyx w array_strptime, to po prostu trzeba dodać (kod p); z może lepszym komunikatem o błędzie dla niezbadanych kodów (lub przekazywaniem do dateutil) – Jeff

Odpowiedz

3

Można obejść ten problem to_datetime, resetując indeks, manipulując serią poprzez mapę/lambda/strptime, a następnie ostatecznie ustawiając ponownie indeks.

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 

Nie tak zwięzły, ale ma ten sam efekt. Lub, aby zapakować go w funkcji:

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 
0

Prostszym rozwiązaniem byłoby naprawić łańcuch więc pasuje co to_datetime spodziewa ...

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] 
Powiązane problemy