Skip to content

BUG: parse_dates may have columns not in dataframe #32320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use chain.from_iterable to read parse_dates
  • Loading branch information
sathyz committed Feb 29, 2020
commit 78ff312a2592ec8b3be1ac90f3986ed876782d1d
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ I/O
``coerce_timestamps``; following pyarrow's default allows writing nanosecond
timestamps with ``version="2.0"`` (:issue:`31652`).
- Bug in :class:`HDFStore` that caused it to set to ``int64`` the dtype of a ``datetime64`` column when reading a DataFrame in Python 3 from fixed format written in Python 2 (:issue:`31750`)
- `read_csv` will raise a ``ValueError`` when the columns passed in `parse_dates` is missing in the dataframe. (:issue:`31251`)
- `read_csv` will raise a ``ValueError`` when the columns passed in `parse_dates` are missing in the dataframe (:issue:`31251`)

Plotting
^^^^^^^^
Expand Down
7 changes: 1 addition & 6 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,12 +1444,7 @@ def _validate_parse_dates_presence(self, columns: List[str]) -> None:
# DateGroups = List[ColReference]
# ParseDates = Union[ DateGroups, List[DateGroups],
# Dict[ColReference, DateGroups]]
cols_needed = []
for col in self.parse_dates:
if isinstance(col, list):
cols_needed.extend(col)
else:
cols_needed.append(col)
cols_needed = chain.from_iterable([col if isinstance(col, list) else [col] for col in self.parse_dates ])
elif isinstance(self.parse_dates, dict):
cols_needed = list(chain(*self.parse_dates.values()))
else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,8 +1537,8 @@ def test_hypothesis_delimited_date(date_format, dayfirst, delimiter, test_dateti
),
],
)
def test_missing_column(all_parsers, names, usecols, parse_dates, missing_cols):
"""GH31251 column names provided in parse_dates could be missing."""
def test_missing_parse_dates_column_raises(all_parsers, names, usecols, parse_dates, missing_cols):
# gh-31251 column names provided in parse_dates could be missing.
parser = all_parsers
content = StringIO("date,time,val\n2020-01-31,04:20:32,32\n")
msg = f"Missing column provided to 'parse_dates': '{missing_cols}'"
Expand Down