Python熊猫中的日期时间strptime:出了什么问题?

时间:2016-05-05 05:17:59

标签: python datetime pandas strptime

import datetime as datetime
datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')

生成

  

AttributeError Traceback(最近一次调用   最后)in()         1导入日期时间为datetime   ----> 2 datetime.strptime('2013-01-01 09:10:12','%Y-%m-%d%H:%M:%S')         3 z = minidf ['日期']         4 z

     

AttributeError:'module'对象没有属性'strptime'

我的目标是转换格式仍为数据对象的pandas dataframe列

import datetime as datetime
#datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
z = minidf['Dates']

0     2015-05-13 23:53:00
1     2015-05-13 23:53:00
2     2015-05-13 23:33:00
3     2015-05-13 23:30:00
4     2015-05-13 23:30:00
5     2015-05-13 23:30:00
6     2015-05-13 23:30:00
7     2015-05-13 23:30:00
8     2015-05-13 23:00:00
9     2015-05-13 23:00:00
10    2015-05-13 22:58:00
Name: Dates, dtype: object

奖金问题是,我从一个包含更多列的较大文件中使用pd.read_csv函数获取此列。是否可以传递参数,以便pd.read_csv直接将其转换为dtype: datetime64[ns]格式

3 个答案:

答案 0 :(得分:6)

我认为您可以用来转换to_datetime

print pd.to_datetime('2013-01-01 09:10:12', format='%Y-%m-%d %H:%M:%S')
2013-01-01 09:10:12

print pd.to_datetime('2013-01-01 09:10:12')
2013-01-01 09:10:12

如果您需要在功能read_csv中进行转换,请添加参数parse_dates

df = pd.read_csv('filename',  parse_dates=['Dates'])

样品:

import pandas as pd
import io

temp=u"""Dates
2015-05-13 23:53:00
2015-05-13 23:53:00
2015-05-13 23:33:00
2015-05-13 23:30:00
2015-05-13 23:30:00
2015-05-13 23:30:00
2015-05-13 23:30:00
2015-05-13 23:30:00
2015-05-13 23:00:00
2015-05-13 23:00:00
2015-05-13 22:58:00
"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),  parse_dates=['Dates'])
print df
                 Dates
0  2015-05-13 23:53:00
1  2015-05-13 23:53:00
2  2015-05-13 23:33:00
3  2015-05-13 23:30:00
4  2015-05-13 23:30:00
5  2015-05-13 23:30:00
6  2015-05-13 23:30:00
7  2015-05-13 23:30:00
8  2015-05-13 23:00:00
9  2015-05-13 23:00:00
10 2015-05-13 22:58:00

print df.dtypes
Dates    datetime64[ns]
dtype: object

to_datetime的另一个解决方案:

print pd.to_datetime(df['Dates'])

样品:

print df
                  Dates
0   2015-05-13 23:53:00
1   2015-05-13 23:53:00
2   2015-05-13 23:33:00
3   2015-05-13 23:30:00
4   2015-05-13 23:30:00
5   2015-05-13 23:30:00
6   2015-05-13 23:30:00
7   2015-05-13 23:30:00
8   2015-05-13 23:00:00
9   2015-05-13 23:00:00
10  2015-05-13 22:58:00

print df.dtypes
Dates    object

df['Dates'] = pd.to_datetime(df['Dates'])
print df
                 Dates
0  2015-05-13 23:53:00
1  2015-05-13 23:53:00
2  2015-05-13 23:33:00
3  2015-05-13 23:30:00
4  2015-05-13 23:30:00
5  2015-05-13 23:30:00
6  2015-05-13 23:30:00
7  2015-05-13 23:30:00
8  2015-05-13 23:00:00
9  2015-05-13 23:00:00
10 2015-05-13 22:58:00

print df.dtypes
Dates    datetime64[ns]
dtype: object

答案 1 :(得分:5)

  

AttributeError:' module'对象没有属性' strptime'

strptime不适用于datetime,而是datetime.datetime

>>> from datetime import datetime
>>> datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2013, 1, 1, 9, 10, 12)

答案 2 :(得分:0)

仅导入模块

>>> import datetime
>>> datetime.datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2013, 1, 1, 9, 10, 12)

将类从模块导入当前上下文:

>>> from datetime import datetime
>>> datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2013, 1, 1, 9, 10, 12)
>>>