从 SQL 数据库导入表并按日期过滤行时,将 Pandas 列解析为日期时间 [英] Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date

查看:15
本文介绍了从 SQL 数据库导入表并按日期过滤行时,将 Pandas 列解析为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataFrame,列名为 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 列的示例是 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:Python27libsite-packagesdateutilparser.pyc in parse(timestr, parserinfo, **kwargs)
    695         return parser(parserinfo).parse(timestr, **kwargs)
    696     else:
--> 697         return DEFAULTPARSER.parse(timestr, **kwargs)
    698 
    699 

C:Python27libsite-packagesdateutilparser.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:Python27libsite-packagesdateutilparser.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:Python27libsite-packagesdateutilparser.pyc in split(cls, s)
    141 
    142     def split(cls, s):
--> 143         return list(cls(s))
    144     split = classmethod(split)
    145 

C:Python27libsite-packagesdateutilparser.pyc in next(self)
    135 
    136     def next(self):
--> 137         token = self.get_token()
    138         if token is None:
    139             raise StopIteration

C:Python27libsite-packagesdateutilparser.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 对象?

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 列解析为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆