Hints for Exercise 7¶
Converting a column of date-strings into datetime format¶
In some cases Pandas cannot understand and parse automatically the date information from a column when you read the
data with read_csv() function. In these cases, you need to parse the date information afterwards.
Let’s see an example with following data
DATE, Value
"201401", 1
"201402", 2
"201403", 3
"201404", 5
Let’s try to read the data and parse the date.
In [1]: data = pd.read_csv(fp, sep=',', parse_dates=['DATE'])
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-f20dd3876fdc> in <module>()
----> 1 data = pd.read_csv(fp, sep=',', parse_dates=['DATE'])
~/checkouts/readthedocs.org/user_builds/geo-python-site/envs/2017.1/lib/python3.7/site-packages/pandas/io/parsers.py in read_csv(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, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
684 )
685
--> 686 return _read(filepath_or_buffer, kwds)
687
688
~/checkouts/readthedocs.org/user_builds/geo-python-site/envs/2017.1/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
450
451 # Create the parser.
--> 452 parser = TextFileReader(fp_or_buf, **kwds)
453
454 if chunksize or iterator:
~/checkouts/readthedocs.org/user_builds/geo-python-site/envs/2017.1/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
934 self.options["has_index_names"] = kwds["has_index_names"]
935
--> 936 self._make_engine(self.engine)
937
938 def close(self):
~/checkouts/readthedocs.org/user_builds/geo-python-site/envs/2017.1/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
1166 def _make_engine(self, engine="c"):
1167 if engine == "c":
-> 1168 self._engine = CParserWrapper(self.f, **self.options)
1169 else:
1170 if engine == "python":
~/checkouts/readthedocs.org/user_builds/geo-python-site/envs/2017.1/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1996 kwds["usecols"] = self.usecols
1997
-> 1998 self._reader = parsers.TextReader(src, **kwds)
1999 self.unnamed_cols = self._reader.unnamed_cols
2000
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()
FileNotFoundError: [Errno 2] No such file or directory: '/home/docs/checkouts/readthedocs.org/user_builds/geo-python-site/checkouts/2017.1/source/data/L7/hintData.txt'
In [2]: data.head()