Skip to content

Commit f0c3954

Browse files
jrebackmattip
authored andcommitted
BUG: index_names can be None when processing date conversions
closes pandas-dev#15820 closes pandas-dev#11544
1 parent 40b4e24 commit f0c3954

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

doc/source/whatsnew/v0.19.0.txt

-4
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,13 @@ Other enhancements
517517
- The ``pd.read_json`` and ``DataFrame.to_json`` has gained support for reading and writing json lines with ``lines`` option see :ref:`Line delimited json <io.jsonl>` (:issue:`9180`)
518518
- :func:`read_excel` now supports the true_values and false_values keyword arguments (:issue:`13347`)
519519
- ``groupby()`` will now accept a scalar and a single-element list for specifying ``level`` on a non-``MultiIndex`` grouper. (:issue:`13907`)
520-
<<<<<<< HEAD
521520
- Non-convertible dates in an excel date column will be returned without conversion and the column will be ``object`` dtype, rather than raising an exception (:issue:`10001`).
522521
- ``pd.Timedelta(None)`` is now accepted and will return ``NaT``, mirroring ``pd.Timestamp`` (:issue:`13687`)
523522
- ``pd.read_stata()`` can now handle some format 111 files, which are produced by SAS when generating Stata dta files (:issue:`11526`)
524523
- ``Series`` and ``Index`` now support ``divmod`` which will return a tuple of
525524
series or indices. This behaves like a standard binary operator with regards
526525
to broadcasting rules (:issue:`14208`).
527526

528-
=======
529-
- Re-enable the ``parse_dates`` keyword of ``read_excel`` to parse string columns as dates (:issue:`14326`)
530-
>>>>>>> PR_TOOL_MERGE_PR_14326
531527

532528
.. _whatsnew_0190.api:
533529

doc/source/whatsnew/v0.20.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you
270270

271271
.. _whatsnew_0200.enhancements.other:
272272

273-
Other enhancements
273+
Other Enhancements
274274
^^^^^^^^^^^^^^^^^^
275275

276276
- Integration with the ``feather-format``, including a new top-level ``pd.read_feather()`` and ``DataFrame.to_feather()`` method, see :ref:`here <io.feather>`.
@@ -314,6 +314,7 @@ Other enhancements
314314
- ``pd.types.concat.union_categoricals`` gained the ``ignore_ordered`` argument to allow ignoring the ordered attribute of unioned categoricals (:issue:`13410`). See the :ref:`categorical union docs <categorical.union>` for more information.
315315
- ``pandas.io.json.json_normalize()`` with an empty ``list`` will return an empty ``DataFrame`` (:issue:`15534`)
316316
- ``pd.DataFrame.to_latex`` and ``pd.DataFrame.to_string`` now allow optional header aliases. (:issue:`15536`)
317+
- Re-enable the ``parse_dates`` keyword of ``read_excel`` to parse string columns as dates (:issue:`14326`)
317318

318319
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
319320

pandas/io/excel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def _parse_excel(self, sheetname=0, header=0, skiprows=None, names=None,
344344
raise NotImplementedError("chunksize keyword of read_excel "
345345
"is not implemented")
346346

347-
if parse_dates is True and not index_col:
347+
if parse_dates is True and index_col is None:
348348
warn("The 'parse_dates=True' keyword of read_excel was provided"
349349
" without an 'index_col' keyword value.")
350350

pandas/io/parsers.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1176,13 +1176,18 @@ def _should_parse_dates(self, i):
11761176
if isinstance(self.parse_dates, bool):
11771177
return self.parse_dates
11781178
else:
1179-
name = self.index_names[i]
1179+
if self.index_names is not None:
1180+
name = self.index_names[i]
1181+
else:
1182+
name = None
11801183
j = self.index_col[i]
11811184

11821185
if is_scalar(self.parse_dates):
1183-
return (j == self.parse_dates) or (name == self.parse_dates)
1186+
return ((j == self.parse_dates) or
1187+
(name is not None and name == self.parse_dates))
11841188
else:
1185-
return (j in self.parse_dates) or (name in self.parse_dates)
1189+
return ((j in self.parse_dates) or
1190+
(name is not None and name in self.parse_dates))
11861191

11871192
def _extract_multi_indexer_columns(self, header, index_names, col_names,
11881193
passed_names=False):
@@ -1352,6 +1357,7 @@ def _get_name(icol):
13521357

13531358
def _agg_index(self, index, try_parse_dates=True):
13541359
arrays = []
1360+
13551361
for i, arr in enumerate(index):
13561362

13571363
if (try_parse_dates and self._should_parse_dates(i)):
@@ -1512,6 +1518,7 @@ def _cast_types(self, values, cast_type, column):
15121518

15131519
def _do_date_conversions(self, names, data):
15141520
# returns data, columns
1521+
15151522
if self.parse_dates is not None:
15161523
data, names = _process_date_conversion(
15171524
data, self._date_conv, self.parse_dates, self.index_col,

pandas/tests/io/test_excel.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -938,12 +938,17 @@ def test_read_excel_parse_dates(self):
938938
res = read_excel(pth)
939939
tm.assert_frame_equal(df2, res)
940940

941-
res = read_excel(pth, parse_dates=['date_strings'])
941+
# no index_col specified when parse_dates is True
942+
with tm.assert_produces_warning():
943+
res = read_excel(pth, parse_dates=True)
944+
tm.assert_frame_equal(df2, res)
945+
946+
res = read_excel(pth, parse_dates=['date_strings'], index_col=0)
942947
tm.assert_frame_equal(df, res)
943948

944949
dateparser = lambda x: pd.datetime.strptime(x, '%m/%d/%Y')
945950
res = read_excel(pth, parse_dates=['date_strings'],
946-
date_parser=dateparser)
951+
date_parser=dateparser, index_col=0)
947952
tm.assert_frame_equal(df, res)
948953

949954
def test_read_excel_skiprows_list(self):

0 commit comments

Comments
 (0)