I am using Pandas 0.8.1 to fetch Yahoo stock price
from datetime import datetime
from pandas.io.data import DataReader
stk_price = DataReader('600809.ss', 'yahoo', datetime(2006,1,1), datetime(2012,12,31)).reset_index()
>>> stk_price.Date
0 2010-01-04 00:00:00
1 2010-01-05 00:00:00
2 2010-01-06 00:00:00
3 2010-01-07 00:00:00
4 2010-01-08 00:00:00
And I want to convert the Date to string by:
>>>stk_price.Date.astype('|S10')
0 1970-01-15
1 1970-01-15
2 1970-01-15
3 1970-01-15
4 1970-01-15
5 1970-01-15
Why it shows “1970-01-15” instead of “2010-01-04” etc ? How to fix it?
And if I have a
DATE_LIST = [
u'20090331', u'20090630', u'20090930', u'20091231', \
u'20100331', u'20100630', u'20100930', u'20101231', \
u'20110331', u'20110630', u'20110930', u'20111231', \
u'20120331', u'20120630', u'20120930', u'20121231'
]
I just try to filter the rows of stk_price whose Date column is in DATE_LIST by below:
from datetime import datetime
from pandas.io.data import DataReader
import numpy as np
DATE_LIST = [
u'20090331', u'20090630', u'20090930', u'20091231', \
u'20100331', u'20100630', u'20100930', u'20101231', \
u'20110331', u'20110630', u'20110930', u'20111231', \
u'20120331', u'20120630', u'20120930', u'20121231'
]
DATE_ARRAY = np.array(DATE_LIST,dtype='datetime64[us]')
stk_price = DataReader('600809.ss', 'yahoo', datetime(2006,1,1), datetime(2012,12,31)).reset_index()
rst = stk_price[stk_price.Date.isin(DATE_ARRAY)].Close
but the rst is empty .
How to fix it or any Pandas function can filter the result ?
Instead of converting stk_price dates to strings (which shows the wrong dates due to a numpy bug, see also github issue #1802) you can also convert the DATE_LIST to timestamps and use these directly to index stk_price. Below an example on frame containing random data.