Time series forecasting in Python



I am reading this time series article

I got struck in executing this line of code

data = pd.read_csv('AirPassengers.csv', parse_dates='Month', index_col='Month',date_parser=dateparse)

I am getting the error as follows

TypeError                                 Traceback (most recent call last)
<ipython-input-5-dea51b569cba> in <module>()
      1 dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m')
----> 2 data = pd.read_csv('AirPassengers.csv', parse_dates='Month', index_col='Month',date_parser=dateparse)
      3 print data.head()

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    560                     skip_blank_lines=skip_blank_lines)
--> 562         return _read(filepath_or_buffer, kwds)
    564     parser_f.__name__ = name

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    314     # Create the parser.
--> 315     parser = TextFileReader(filepath_or_buffer, **kwds)
    317     if (nrows is not None) and (chunksize is not None):

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, engine, **kwds)
    643             self.options['has_index_names'] = kwds['has_index_names']
--> 645         self._make_engine(self.engine)
    647     def close(self):

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _make_engine(self, engine)
    797     def _make_engine(self, engine='c'):
    798         if engine == 'c':
--> 799             self._engine = CParserWrapper(self.f, **self.options)
    800         else:
    801             if engine == 'python':

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in __init__(self, src, **kwds)
   1201         self.as_recarray = kwds.get('as_recarray', False)
-> 1202         ParserBase.__init__(self, kwds)
   1204         if 'utf-16' in (kwds.get('encoding') or ''):

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in __init__(self, kwds)
    892         self.parse_dates = _validate_parse_dates_arg(
--> 893             kwds.pop('parse_dates', False))
    894         self.date_parser = kwds.pop('date_parser', None)
    895         self.dayfirst = kwds.pop('dayfirst', False)

C:\Users\JAYASHREE\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _validate_parse_dates_arg(parse_dates)
    871         if lib.isscalar(parse_dates):
    872             if not lib.is_bool(parse_dates):
--> 873                 raise TypeError(msg)
    875         elif not isinstance(parse_dates, (list, dict)):

TypeError: Only booleans, lists, and dictionaries are accepted for the 'parse_dates' parameter

Kindly help me solve the problem.Thanks


Hi there!

Give this a try
data = pd.read_csv(‘AirPassengers.csv’, parse_dates=[‘Month’], index_col=‘Month’,

And let me know if this works.
My guess, to the solution was, that it asked for a list, so we would prefer to use a list, rather than string reference.
Here is more input to this:

Stack Overflow - Parse Dates not working


Hi @krshrimali,
Yes it is working.Thanks for the timely help.