Skip to content

Commit 321ec2f

Browse files
committed
BUG: Enforce parse_dates as bool when scalar
Closes gh-5636. In addition, this commit also adds validation to ensure that parse_dates is one of bool, list, or dict.
1 parent bac68d6 commit 321ec2f

File tree

3 files changed

+473
-420
lines changed

3 files changed

+473
-420
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ API changes
134134

135135
- ``Series.apply`` for category dtype now applies passed function to each ``.categories`` (not ``.codes``), and returns "category" dtype if possible (:issue:`12473`)
136136

137+
- ``read_csv`` will now raise a ``TypeError`` if ``parse_dates`` is neither a boolean, list, or dictionary (:issue:`5636`)
137138

138139
- The default for ``.query()/.eval()`` is now ``engine=None``, which will use ``numexpr`` if it's installed; otherwise it will fallback to the ``python`` engine. This mimics the pre-0.18.1 behavior if ``numexpr`` is installed (and which Previously, if numexpr was not installed, ``.query()/.eval()`` would raise). (:issue:`12749`)
139140

pandas/io/parsers.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,27 @@ def _validate_usecols_arg(usecols):
825825
return usecols
826826

827827

828+
def _validate_parse_dates_arg(parse_dates):
829+
"""
830+
Check whether or not the 'parse_dates' parameter
831+
is a non-boolean scalar. Raises a ValueError if
832+
that is the case.
833+
"""
834+
msg = ("Only booleans, lists, and "
835+
"dictionaries are accepted "
836+
"for the 'parse_dates' parameter")
837+
838+
if parse_dates is not None:
839+
if lib.isscalar(parse_dates):
840+
if not lib.is_bool(parse_dates):
841+
raise TypeError(msg)
842+
843+
elif not isinstance(parse_dates, (list, dict)):
844+
raise TypeError(msg)
845+
846+
return parse_dates
847+
848+
828849
class ParserBase(object):
829850

830851
def __init__(self, kwds):
@@ -836,7 +857,8 @@ def __init__(self, kwds):
836857
self.index_names = None
837858
self.col_names = None
838859

839-
self.parse_dates = kwds.pop('parse_dates', False)
860+
self.parse_dates = _validate_parse_dates_arg(
861+
kwds.pop('parse_dates', False))
840862
self.date_parser = kwds.pop('date_parser', None)
841863
self.dayfirst = kwds.pop('dayfirst', False)
842864
self.keep_date_col = kwds.pop('keep_date_col', False)

0 commit comments

Comments
 (0)