I have a pandas DataFrame with index in datetime64[ns] and when I convert it to generator, my datetime is changed to timestamp. How can I avoid this?
Here the code. (I also tried itertuples
instead of iterrow
, but got the same result).
>>> test.index
DatetimeIndex(['1990-01-02', '1990-01-03', '1990-01-04', '1990-01-05',
'1990-01-08', '1990-01-09', '1990-01-10', '1990-01-11',
'1990-01-12', '1990-01-15',
...
'2015-11-05', '2015-11-06', '2015-11-09', '2015-11-10',
'2015-11-11', '2015-11-12', '2015-11-13', '2015-11-16',
'2015-11-17', '2015-11-18'],
dtype='datetime64[ns]', name=u'datetime', length=6524, freq=None, tz=None)
>>> test.iterrows()
<generator object iterrows at 0x104314640>
>>> test.iterrows().next()
(Timestamp('1990-01-02 00:00:00'), open 35.249999
high 37.500000
low 35.000000
close 37.250001
volume 45799600.000000
adj_close 1.157262
Name: 1990-01-02 00:00:00, dtype: float64)
actually this is because your index dtype
is datetime
, to convert it to date
you can do the following
df.index = df.index.date
In [68]:
dates = ['1990-01-02', '1990-01-03', '1990-01-04', '1990-01-05',
'1990-01-08', '1990-01-09', '1990-01-10', '1990-01-11',
'1990-01-12', '1990-01-15',
'2015-11-05', '2015-11-06', '2015-11-09', '2015-11-10',
'2015-11-11', '2015-11-12', '2015-11-13', '2015-11-16',
'2015-11-17', '2015-11-18']
In [90]:
df = pd.DataFrame(np.arange(5).reshape(-1 ,1) , columns = ['data'] , index = [ dt.datetime.strptime(date , '%Y-%m-%d') for date in dates[0:5]] )
df
Out[90]:
data
1990-01-02 0
1990-01-03 1
1990-01-04 2
1990-01-05 3
1990-01-08 4
In [91]:
df.index
Out[91]:
DatetimeIndex(['1990-01-02', '1990-01-03', '1990-01-04', '1990-01-05',
'1990-01-08'],
dtype='datetime64[ns]', freq=None)
In [92]:
df.index = df.index.date
df.index
Out[92]:
Index([1990-01-02, 1990-01-03, 1990-01-04, 1990-01-05, 1990-01-08], dtype='object')
In [88]:
df.iterrows().next()
Out[88]:
(datetime.date(1990, 1, 2), a 0
b 1
c 2
d 3
e 4
Name: 1990-01-02, dtype: int32)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.