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

Datetime strptime in Python pandas : what's wrong?

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

生产

AttributeError Traceback (most recent call
last) in ()
1 import datetime as datetime
----> 2 datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
3 z = minidf['Dates']
4 z

AttributeError: 'module' object has no attribute 'strptime'

我的目标是转换一个格式仍然是数据对象的熊猫数据帧列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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]格式?


我想你可以用来转换to_datetime

1
2
3
4
5
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

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

样品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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的另一个解决方案:

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

样品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

AttributeError: 'module' object has no attribute 'strptime'

strptimedatetime上不可用,但在datetime.datetime上可用。

1
2
3
>>> 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)


仅导入模块

1
2
3
>>> 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)

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

1
2
3
4
>>> 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)
>>>