Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date

pandas series to datetime
pandas datetime to date
pandas remove time from datetime
pd to_datetime format dd/mm/yyyy
pandas strptime
convert object to datetime python
pandas read column as datetime
pandas timestamp to datetime

I have a DataFrame with column named date. How can we convert/parse the 'date' column to a DateTime object?

I loaded the date column from a Postgresql database using sql.read_frame(). An example of the date column is 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.

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>()
     16 # Parse 'Date' Column to Datetime
---> 17 df['date'] = dateutil.parser.parse(df['date'])

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)

C:\Python27\lib\site-packages\dateutil\parser.pyc in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    299             default =, 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:

C:\Python27\lib\site-packages\dateutil\parser.pyc in split(cls, s)
    142     def split(cls, s):
--> 143         return list(cls(s))
    144     split = classmethod(split)

C:\Python27\lib\site-packages\dateutil\parser.pyc in next(self)
    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 =
     69                 while nextchar == '\x00':
     70                     nextchar =

AttributeError: 'Series' object has no attribute 'read'

df['date'].apply(dateutil.parser.parse) gives me the error AttributeError: '' object has no attribute 'read'

df['date'].truncate(after='2013/04/01') gives the error TypeError: can't compare datetime.datetime to long

df['date'].dtype returns dtype('O'). Is it already a datetime object?

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[( > '2012-04-01') & ( < '2012-04-04')]

Because you said that you were getting an error from the string for some reason, try this:

df_masked = df[( >,4,1)) & ( <,4,4))]

How to change the Pandas datetime format in Python, argint, float, str, datetime, list, tuple, 1-d array, Series DataFrame/dict-like If True​, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. The numeric values would be parsed as number of units (defined by unit ) since this  You could load df into a database table, using df.to_sql: df.to_sql(tablename, engine, if_exists='replace') and then perform the join in the database, and read the result into a new DataFrame using read_sql: sql = '''select * from Table-A a left join Table-B b on (a.sec1 = b.sec1) where DATE_SUB(CURDATE(),INTERVAL 12 MONTH)

Pandas is aware of the object datetime but when you use some of the import functions it is taken as a string. So what you need to do is make sure the column is set as the datetime type not as a string. Then you can make your query.

df['date']  = pd.to_datetime(df['date'])
df_masked = df[(df['date'] >,4,1)) & (df['date'] <,4,4))]

How to Convert DataFrame Column to Datetime in Pandas, Table Of Contents pandas Ecosystem · Comparison with R / R libraries · Comparison with SQL · Comparison arg : integer, float, string, datetime, list, tuple, 1-d array, Series If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. If True returns a DatetimeIndex; If False returns ndarray of values. Parse date from two columns pandas. Ask Question Asked 4 years, 2 months ago. Active 2 years, 3 months ago. Viewed 886 times 1. 0. I have a set of data that looks like this (3 columns). The date and time are in 1 column and the timezone is in another col

You probably need apply, so something like:

df['date'] = df['date'].apply(dateutil.parser.parse)

Without an example of the column I can't guarantee this will work, but something in that direction should help you to carry on.

Python, , the entire column or index will be returned unaltered as an object data type. Loading data from a database into a Pandas DataFrame is surprisingly easy. To load an entire table, use the read_sql_table () method: sql_DF = pd.read_sql_table ("nyc_jobs", con=engine) The first two parameters we pass are the same as last time: first is our table name, and then our SQLAlchemy engine. The above snippet is perhaps the quickest

How do I change the date format in a data frame? pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=True, parse_dates=None, columns=None, chunksize=None) [source] ¶ Read SQL database table into a DataFrame. Given a table name and a SQLAlchemy connectable, returns a DataFrame. This function does not support DBAPI connections. Name of SQL table in database.

You should iterate over the items and parse them independently, then construct a new list.

df['date'] = [dateutil.parser.parse(x) for x in df['date']]

. Use "%m" to indicate where the month should be positioned, "%d" for the day, and "%y" for the year. Convert argument to datetime. Specify a date parse order if arg is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior). Specify a date parse order if arg is str or its

How do I convert a column to a date in python? Time series / date functionality¶. pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.timeseries as well as created a tremendous amount of new functionality for manipulating time series data.

How do you convert an object to time in python? pandas.read_sql¶ pandas.read_sql (sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, columns=None, chunksize=None) [source] ¶ Read SQL query or database table into a DataFrame. This function is a convenience wrapper around read_sql_table and read_sql_query (for backward compatibility). It will delegate to the specific

I am sharing the table of content in case you are just interested to see a specific Import time-series data; Extract Month and Year from datetime using We are using parse_date attribute to parse and convert the date columns in the csv Filter all rows between two dates i.e. 1989-JAN and 1995-Apr here. I have two columns, one with and one with datetime.time, which both exhibited this problem. I force-converted the column via pd.to_datetime into a datetimeindex, which to_sql/sqlalchemy correctly formats into an SQL-acceptable date format. However, to_datetime does not work on, leaving the pandas

  • Please post an example of something in your date column! Because pandas should actually recognize a datetime object, so it would be beneficial to see the actual format for that column
  • @RyanSaxe I loaded the date column from a Postgresql database using sql.read_frame(). An example of the date column is 2013-04-04. How do you check for the dtype of a column?
  • df['date'].dtype returns dtype('O')
  • df = df[ > '2012-01-01'] gives me an error TypeError: can't compare to str.
  • I use this all the time! That's very odd...your question is very similar to one I asked and I was given this answer and it worked. See it here
  • Yes.. it works when I created the dataframe manually... but if I create the dataframe from a SQL database using sql.read_frame, '2012-01-01' gets treated as a string?
  • Trying df[ > dateutil.parser.parse('2013-01-01') ] gives me TypeError: can't compare datetime.datetime to
  • is type object, but i think 2013-01-01 is treated as a string. The error changes from having a str to having a when I used dateutil.parser.parse() as in above comments
  • Thanks, I tried df['date'].apply(dateutil.parser.parse) and it gave ethe error. AttributeError: '' object has no attribute 'read'. An example of the column is 2013-04-04. The entire dataframe was loaded from a PostgreSQL database using sql.readframe().