Python pandas 中的日期时间 strptime:怎么了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37042922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datetime strptime in Python pandas : what's wrong?
提问by Fagui Curtain
import datetime as datetime
datetime.strptime('2013-01-01 09:10:12', '%Y-%m-%d %H:%M:%S')
produces
产生
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'
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' 对象没有属性 'strptime'
my goal is to convert a pandas dataframe column whose format is still a data object
我的目标是转换格式仍然是数据对象的熊猫数据框列
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
the bonus question is, i got this column using pd.read_csv
function from a larger file with more columns. Is it possible to pass parameters such that pd.read_csv
directly converts this to dtype: datetime64[ns]
format
额外的问题是,我使用pd.read_csv
来自具有更多列的更大文件的函数获得了此列。是否可以传递参数,以便pd.read_csv
直接将其转换为dtype: datetime64[ns]
格式
回答by jezrael
I think you can use for converting to_datetime
:
我认为您可以用于转换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
If you need convert in function read_csv
, add parameter parse_dates
:
如果需要在函数中转换read_csv
,请添加参数parse_dates
:
df = pd.read_csv('filename', parse_dates=['Dates'])
Sample:
样本:
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
Another solution with to_datetime
:
另一个解决方案to_datetime
:
print pd.to_datetime(df['Dates'])
Sample:
样本:
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
回答by AKS
AttributeError: 'module' object has no attribute 'strptime'
AttributeError: 'module' 对象没有属性 'strptime'
strptime
is not available on datetime
but on datetime.datetime
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)
回答by riteshtch
importing only the module
仅导入模块
>>> 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)
importing the class from the module into current context:
将类从模块导入当前上下文:
>>> 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)
>>>