Skip to content

Commit 98db2b5

Browse files
author
MarcoGorelli
committed
📝 add example of reading csv file with mixed formats
1 parent d67bd35 commit 98db2b5

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

doc/source/user_guide/io.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,14 @@ format.
10231023
df
10241024
10251025
In the case that you have mixed datetime formats within the same column, you'll need to
1026-
first read it in the file, and then apply :func:`to_datetime` to each element.
1026+
first read it in as an object dtype and then apply :func:`to_datetime` to each element.
1027+
1028+
.. ipython:: python
1029+
1030+
data = io.StringIO("date\n12 Jan 2000\n2000-01-13\n")
1031+
df = pd.read_csv(data)
1032+
df['date'] = df['date'].apply(pd.to_datetime)
1033+
df
10271034
10281035
.. ipython:: python
10291036
:suppress:

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Datetimes are now parsed with a consistent format
118118
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119119

120120
:func:`to_datetime` now parses dates with a consistent format, which is guessed from the first non-NA value
121-
(unless ``format`` is specified). Previously, it would've guessed the format for each element individually.
121+
(unless ``format`` is specified). Previously, it would have guessed the format for each element individually.
122122

123123
*Old behavior*:
124124

pandas/_libs/tslibs/parsing.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1117,13 +1117,13 @@ cdef void _maybe_warn_about_dayfirst(format: str, bint dayfirst):
11171117
if (day_index > month_index) and dayfirst:
11181118
warnings.warn(
11191119
f"Parsing dates in {format} format when dayfirst=True was specified. "
1120-
f"Pass `dayfirst=False` or specify a format to silence this warning.",
1120+
"Pass `dayfirst=False` or specify a format to silence this warning.",
11211121
stacklevel=find_stack_level(),
11221122
)
11231123
if (day_index < month_index) and not dayfirst:
11241124
warnings.warn(
11251125
f"Parsing dates in {format} format when dayfirst=False was specified. "
1126-
f"Pass `dayfirst=True` or specify a format to silence this warning.",
1126+
"Pass `dayfirst=True` or specify a format to silence this warning.",
11271127
stacklevel=find_stack_level(),
11281128
)
11291129

0 commit comments

Comments
 (0)