Skip to content

Commit 6e6d579

Browse files
author
MarcoGorelli
committed
improve message, use if-statement
1 parent 8952a0e commit 6e6d579

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

doc/source/user_guide/io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ first read it in as an object dtype and then apply :func:`to_datetime` to each e
10111011
df['date'] = df['date'].apply(pd.to_datetime)
10121012
df
10131013
1014-
or, if your datetime formats are all ISO8601:
1014+
or, if your datetime formats are all ISO8601 (but possibly not identically-formatted):
10151015

10161016
.. ipython:: python
10171017

doc/source/whatsnew/v2.0.0.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Other enhancements
168168
- Improved error message when trying to align :class:`DataFrame` objects (for example, in :func:`DataFrame.compare`) to clarify that "identically labelled" refers to both index and columns (:issue:`50083`)
169169
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`50616`)
170170
- Added new argument ``dtype`` to :func:`read_sql` to be consistent with :func:`read_sql_query` (:issue:`50797`)
171-
- :func:`to_datetime` now accepts ``"ISO8601"`` as an argument to ``format``, which will match any ISO8601 string (:issue:`50411`)
171+
- :func:`to_datetime` now accepts ``"ISO8601"`` as an argument to ``format``, which will match any ISO8601 string (but possibly not identically-formatted) (:issue:`50411`)
172172
-
173173

174174
.. ---------------------------------------------------------------------------
@@ -560,7 +560,7 @@ to each element individually, e.g. ::
560560
ser = pd.Series(['13-01-2000', '12 January 2000'])
561561
ser.apply(pd.to_datetime)
562562

563-
or, if your formats are all ISO8601, ::
563+
or, if your formats are all ISO8601 (but possibly not identically-formatted) ::
564564

565565
ser = pd.Series(['2020-01-01', '2020-01-01 03:00'])
566566
pd.to_datetime(ser, format='ISO8601')

pandas/_libs/tslibs/strptime.pyx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,40 @@ def array_strptime(
349349
if exact:
350350
found = format_regex.match(val)
351351
if not found:
352-
raise ValueError(
353-
f"time data \"{val}\" doesn't "
354-
f"match {'(inferred) '*fmt_inferred}format \"{fmt}\""
355-
)
352+
if fmt_inferred:
353+
raise ValueError(
354+
f"time data \"{val}\" doesn't "
355+
f"match (inferred) format \"{fmt}\""
356+
)
357+
else:
358+
raise ValueError(
359+
f"time data \"{val}\" doesn't match format \"{fmt}\""
360+
)
356361
if len(val) != found.end():
357-
raise ValueError(
358-
"unconverted data remains when parsing with "
359-
f"{'(inferred) '*fmt_inferred}format \"{fmt}\": "
360-
f'"{val[found.end():]}"'
361-
)
362+
if fmt_inferred:
363+
raise ValueError(
364+
"unconverted data remains when parsing with "
365+
f"(inferred) format \"{fmt}\": \"{val[found.end():]}\""
366+
)
367+
else:
368+
raise ValueError(
369+
"unconverted data remains when parsing with "
370+
f"format \"{fmt}\": \"{val[found.end():]}\""
371+
)
362372

363373
# search
364374
else:
365375
found = format_regex.search(val)
366376
if not found:
367-
raise ValueError(
368-
f"time data \"{val}\" doesn't match "
369-
f"{'(inferred) '*fmt_inferred}format \"{fmt}\""
370-
)
377+
if fmt_inferred:
378+
raise ValueError(
379+
f"time data \"{val}\" doesn't match "
380+
f"(inferred) format \"{fmt}\""
381+
)
382+
else:
383+
raise ValueError(
384+
f"time data \"{val}\" doesn't match format \"{fmt}\""
385+
)
371386

372387
iso_year = -1
373388
year = 1900

0 commit comments

Comments
 (0)