我有一个DataFrame
名为date
. 我们如何将“日期”列转换/解析为DateTime
对象?
我使用sql.read_frame()
. date
该列的一个示例是2013-04-04
。
我想要做的是选择数据框中的所有行,这些行在特定时期内具有日期列,例如 after2013-04-01
和 before 2013-04-04
。
我在下面的尝试给出了错误'Series' object has no attribute 'read'
试图
import dateutil
df['date'] = dateutil.parser.parse(df['date'])
错误
AttributeError Traceback (most recent call last)
<ipython-input-636-9b19aa5f989c> in <module>()
15
16 # Parse 'Date' Column to Datetime
---> 17 df['date'] = dateutil.parser.parse(df['date'])
18
19 # SELECT RECENT SALES
C:\Python27\lib\site-packages\dateutil\parser.pyc in parse(timestr, parserinfo, **kwargs)
695 return parser(parserinfo).parse(timestr, **kwargs)
696 else:
--> 697 return DEFAULTPARSER.parse(timestr, **kwargs)
698
699
C:\Python27\lib\site-packages\dateutil\parser.pyc in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
299 default = datetime.datetime.now().replace(hour=0, minute=0,
300 second=0, microsecond=0)
--> 301 res = self._parse(timestr, **kwargs)
302 if res is None:
303 raise ValueError, "unknown string format"
C:\Python27\lib\site-packages\dateutil\parser.pyc in _parse(self, timestr, dayfirst, yearfirst, fuzzy)
347 yearfirst = info.yearfirst
348 res = self._result()
--> 349 l = _timelex.split(timestr)
350 try:
351
C:\Python27\lib\site-packages\dateutil\parser.pyc in split(cls, s)
141
142 def split(cls, s):
--> 143 return list(cls(s))
144 split = classmethod(split)
145
C:\Python27\lib\site-packages\dateutil\parser.pyc in next(self)
135
136 def next(self):
--> 137 token = self.get_token()
138 if token is None:
139 raise StopIteration
C:\Python27\lib\site-packages\dateutil\parser.pyc in get_token(self)
66 nextchar = self.charstack.pop(0)
67 else:
---> 68 nextchar = self.instream.read(1)
69 while nextchar == '\x00':
70 nextchar = self.instream.read(1)
AttributeError: 'Series' object has no attribute 'read'
df['date'].apply(dateutil.parser.parse)
给我错误AttributeError: 'datetime.date' object has no attribute 'read'
df['date'].truncate(after='2013/04/01')
给出错误TypeError: can't compare datetime.datetime to long
df['date'].dtype
返回dtype('O')
。它已经是一个datetime
对象了吗?