从SQL数据库导入表并按日期过滤行时,将Pandas列解析为Datetime [英] Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date
问题描述
我有一个DataFrame
,列名为date
.我们如何将'date'列转换/解析为DateTime
对象?
I have a DataFrame
with column named date
. How can we convert/parse the 'date' column to a DateTime
object?
我使用sql.read_frame()
从Postgresql数据库中加载了date列. date
列的示例是2013-04-04
.
I loaded the date column from a Postgresql database using sql.read_frame()
. An example of the date
column is 2013-04-04
.
我想做的是选择某个日期范围内具有日期列的数据框中的所有行,例如2013-04-01
之后和2013-04-04
之前.
What I am trying to do is to select all rows in a dataframe that has their date columns within a certain period, like after 2013-04-01
and before 2013-04-04
.
我在下面的尝试给出错误'Series' object has no attribute 'read'
My attempt below gives the error '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'].apply(dateutil.parser.parse)
gives me the error 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
对象了吗?
df['date'].dtype
returns dtype('O')
. Is it already a datetime
object?
推荐答案
pandas已将其读取为datetime
对象!因此,您想要的是在两个日期之间选择行,然后可以通过遮罩来做到这一点:
pandas already reads that as a datetime
object! So what you want is to select rows between two dates and you can do that by masking:
df_masked = df[(df.date > '2012-04-01') & (df.date < '2012-04-04')]
由于您说由于某种原因从字符串中获取错误,请尝试以下操作:
Because you said that you were getting an error from the string for some reason, try this:
df_masked = df[(df.date > datetime.date(2012,4,1)) & (df.date < datetime.date(2012,4,4))]
这篇关于从SQL数据库导入表并按日期过滤行时,将Pandas列解析为Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!