Skip to content

Commit 78ff312

Browse files
committed
use chain.from_iterable to read parse_dates
1 parent 3a99b39 commit 78ff312

File tree

3 files changed

+4
-9
lines changed

3 files changed

+4
-9
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ I/O
296296
``coerce_timestamps``; following pyarrow's default allows writing nanosecond
297297
timestamps with ``version="2.0"`` (:issue:`31652`).
298298
- 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`)
299-
- `read_csv` will raise a ``ValueError`` when the columns passed in `parse_dates` is missing in the dataframe. (:issue:`31251`)
299+
- `read_csv` will raise a ``ValueError`` when the columns passed in `parse_dates` are missing in the dataframe (:issue:`31251`)
300300

301301
Plotting
302302
^^^^^^^^

pandas/io/parsers.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,12 +1444,7 @@ def _validate_parse_dates_presence(self, columns: List[str]) -> None:
14441444
# DateGroups = List[ColReference]
14451445
# ParseDates = Union[ DateGroups, List[DateGroups],
14461446
# Dict[ColReference, DateGroups]]
1447-
cols_needed = []
1448-
for col in self.parse_dates:
1449-
if isinstance(col, list):
1450-
cols_needed.extend(col)
1451-
else:
1452-
cols_needed.append(col)
1447+
cols_needed = chain.from_iterable([col if isinstance(col, list) else [col] for col in self.parse_dates ])
14531448
elif isinstance(self.parse_dates, dict):
14541449
cols_needed = list(chain(*self.parse_dates.values()))
14551450
else:

pandas/tests/io/parser/test_parse_dates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,8 +1537,8 @@ def test_hypothesis_delimited_date(date_format, dayfirst, delimiter, test_dateti
15371537
),
15381538
],
15391539
)
1540-
def test_missing_column(all_parsers, names, usecols, parse_dates, missing_cols):
1541-
"""GH31251 column names provided in parse_dates could be missing."""
1540+
def test_missing_parse_dates_column_raises(all_parsers, names, usecols, parse_dates, missing_cols):
1541+
# gh-31251 column names provided in parse_dates could be missing.
15421542
parser = all_parsers
15431543
content = StringIO("date,time,val\n2020-01-31,04:20:32,32\n")
15441544
msg = f"Missing column provided to 'parse_dates': '{missing_cols}'"

0 commit comments

Comments
 (0)