Skip to content

Commit a2ba350

Browse files
committed
DOC: Clarify date_format usage in read_csv documentation specifically pandas/io/parsers/readers.py issue pandas-dev#59557
1 parent c4467a9 commit a2ba350

File tree

1 file changed

+29
-67
lines changed

1 file changed

+29
-67
lines changed

pandas/io/parsers/readers.py

+29-67
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,40 @@
66

77
from __future__ import annotations
88

9-
from collections import (
10-
abc,
11-
defaultdict,
12-
)
139
import csv
1410
import sys
15-
from textwrap import fill
16-
from typing import (
17-
IO,
18-
TYPE_CHECKING,
19-
Any,
20-
Generic,
21-
Literal,
22-
TypedDict,
23-
overload,
24-
)
2511
import warnings
12+
from collections import abc, defaultdict
13+
from textwrap import fill
14+
from typing import IO, TYPE_CHECKING, Any, Generic, Literal, TypedDict, overload
2615

2716
import numpy as np
2817

18+
from pandas import Series
2919
from pandas._libs import lib
3020
from pandas._libs.parsers import STR_NA_VALUES
31-
from pandas.errors import (
32-
AbstractMethodError,
33-
ParserWarning,
34-
)
35-
from pandas.util._decorators import Appender
36-
from pandas.util._exceptions import find_stack_level
37-
from pandas.util._validators import check_dtype_backend
38-
3921
from pandas.core.dtypes.common import (
4022
is_file_like,
4123
is_float,
4224
is_integer,
4325
is_list_like,
4426
pandas_dtype,
4527
)
46-
47-
from pandas import Series
4828
from pandas.core.frame import DataFrame
4929
from pandas.core.indexes.api import RangeIndex
5030
from pandas.core.shared_docs import _shared_docs
51-
52-
from pandas.io.common import (
53-
IOHandles,
54-
get_handle,
55-
stringify_path,
56-
validate_header_arg,
57-
)
31+
from pandas.errors import AbstractMethodError, ParserWarning
32+
from pandas.io.common import IOHandles, get_handle, stringify_path, validate_header_arg
5833
from pandas.io.parsers.arrow_parser_wrapper import ArrowParserWrapper
59-
from pandas.io.parsers.base_parser import (
60-
ParserBase,
61-
is_index_col,
62-
parser_defaults,
63-
)
34+
from pandas.io.parsers.base_parser import ParserBase, is_index_col, parser_defaults
6435
from pandas.io.parsers.c_parser_wrapper import CParserWrapper
65-
from pandas.io.parsers.python_parser import (
66-
FixedWidthFieldParser,
67-
PythonParser,
68-
)
36+
from pandas.io.parsers.python_parser import FixedWidthFieldParser, PythonParser
37+
from pandas.util._decorators import Appender
38+
from pandas.util._exceptions import find_stack_level
39+
from pandas.util._validators import check_dtype_backend
6940

7041
if TYPE_CHECKING:
71-
from collections.abc import (
72-
Callable,
73-
Hashable,
74-
Iterable,
75-
Mapping,
76-
Sequence,
77-
)
42+
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
7843
from types import TracebackType
7944

8045
from pandas._typing import (
@@ -138,6 +103,7 @@ class _read_shared(TypedDict, Generic[HashableT], total=False):
138103
float_precision: Literal["high", "legacy", "round_trip"] | None
139104
storage_options: StorageOptions | None
140105
dtype_backend: DtypeBackend | lib.NoDefault
106+
141107
else:
142108
_read_shared = dict
143109

@@ -321,20 +287,18 @@ class _read_shared(TypedDict, Generic[HashableT], total=False):
321287
322288
Note: A fast-path exists for iso8601-formatted dates.
323289
date_format : str or dict of column -> format, optional
324-
Format to use for parsing dates when used in conjunction with ``parse_dates``.
325-
The strftime to parse time, e.g. :const:`"%d/%m/%Y"`. See
326-
`strftime documentation
327-
<https://docs.python.org/3/library/datetime.html
328-
#strftime-and-strptime-behavior>`_ for more information on choices, though
329-
note that :const:`"%f"` will parse all the way up to nanoseconds.
330-
You can also pass:
331-
332-
- "ISO8601", to parse any `ISO8601 <https://en.wikipedia.org/wiki/ISO_8601>`_
333-
time string (not necessarily in exactly the same format);
334-
- "mixed", to infer the format for each element individually. This is risky,
335-
and you should probably use it along with `dayfirst`.
290+
Format to use for parsing dates and/or times when used in conjunction with ``parse_dates``.
291+
This format should be specified using the `strftime` directives (e.g., :const:`"%d/%m/%Y"`).
292+
Refer to the `strftime documentation <https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior>`_
293+
for more details on format choices. Note that :const:`"%f"` will parse all the way up to nanoseconds.
294+
295+
You can also pass the following special values:
296+
297+
- "ISO8601": To parse any `ISO8601 <https://en.wikipedia.org/wiki/ISO_8601>`_ time string (not necessarily in exactly the same format).
298+
- "mixed": To infer the format for each element individually. This is less predictable and should generally be used with `dayfirst` for safer parsing.
336299
337300
.. versionadded:: 2.0.0
301+
338302
dayfirst : bool, default False
339303
DD/MM format dates, international and European format.
340304
cache_dates : bool, default True
@@ -792,10 +756,9 @@ def read_csv(
792756
skipfooter: int = 0,
793757
nrows: int | None = None,
794758
# NA and Missing Data Handling
795-
na_values: Hashable
796-
| Iterable[Hashable]
797-
| Mapping[Hashable, Iterable[Hashable]]
798-
| None = None,
759+
na_values: (
760+
Hashable | Iterable[Hashable] | Mapping[Hashable, Iterable[Hashable]] | None
761+
) = None,
799762
keep_default_na: bool = True,
800763
na_filter: bool = True,
801764
skip_blank_lines: bool = True,
@@ -927,10 +890,9 @@ def read_table(
927890
skipfooter: int = 0,
928891
nrows: int | None = None,
929892
# NA and Missing Data Handling
930-
na_values: Hashable
931-
| Iterable[Hashable]
932-
| Mapping[Hashable, Iterable[Hashable]]
933-
| None = None,
893+
na_values: (
894+
Hashable | Iterable[Hashable] | Mapping[Hashable, Iterable[Hashable]] | None
895+
) = None,
934896
keep_default_na: bool = True,
935897
na_filter: bool = True,
936898
skip_blank_lines: bool = True,

0 commit comments

Comments
 (0)