datetime转为date,pandas的日期类型转为python的datime

dataframe的数据格式是这样子的:

d1.PNG

 
info看一下里面的数据类型:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 307 entries, 0 to 306
Data columns (total 7 columns):
日期 307 non-null datetime64[ns]
指数 307 non-null float64
成交额(亿元) 307 non-null float64
涨跌 307 non-null float64
涨跌额 307 non-null float64
转债数目 307 non-null float64
剩余规模 307 non-null float64
dtypes: datetime64[ns](1), float64(6)
memory usage: 16.9 KB

日期 307 non-null datetime64[ns]
 
然后转为list看看:
a=list(df['日期'].values)
如果使用上面的方法,返回的是这样的数据:
[numpy.datetime64('2017-12-29T00:00:00.000000000'),
numpy.datetime64('2018-01-02T00:00:00.000000000'),
numpy.datetime64('2018-01-03T00:00:00.000000000'),
numpy.datetime64('2018-01-04T00:00:00.000000000'),
numpy.datetime64('2018-01-05T00:00:00.000000000'),
numpy.datetime64('2018-01-08T00:00:00.000000000'),
numpy.datetime64('2018-01-09T00:00:00.000000000'),
numpy.datetime64('2018-01-10T00:00:00.000000000'),
numpy.datetime64('2018-01-11T00:00:00.000000000'),
numpy.datetime64('2018-01-12T00:00:00.000000000'),
numpy.datetime64('2018-01-15T00:00:00.000000000'),
numpy.datetime64('2018-01-16T00:00:00.000000000'),
numpy.datetime64('2018-01-17T00:00:00.000000000'),

 
如何转化为python的daetime格式呢?
 
可以使用内置的:s.dt.to_pydatetime()
s为df的一列,也就是series数据格式
 
b=list(df['日期'].dt.to_pydatetime())
得到的是
[datetime.datetime(2017, 12, 29, 0, 0),
datetime.datetime(2018, 1, 2, 0, 0),
datetime.datetime(2018, 1, 3, 0, 0),
datetime.datetime(2018, 1, 4, 0, 0),
datetime.datetime(2018, 1, 5, 0, 0),
datetime.datetime(2018, 1, 8, 0, 0),
datetime.datetime(2018, 1, 9, 0, 0),
datetime.datetime(2018, 1, 10, 0, 0),
datetime.datetime(2018, 1, 11, 0, 0),
datetime.datetime(2018, 1, 12, 0, 0),
datetime.datetime(2018, 1, 15, 0, 0)

为了不想要小时,分钟,秒的数据,可以清洗一下:
b=[i.strftime('%Y-%m-%d') for i in b]
 
得到:
['2017-12-29',
'2018-01-02',
'2018-01-03',
'2018-01-04',
'2018-01-05',
'2018-01-08',
'2018-01-09',
'2018-01-10',
'2018-01-11',
'2018-01-12',
'2018-01-15',
'2018-01-16',
'2018-01-17',
 
 

0 个评论

要回复文章请先登录注册