Skip to content

Commit a28df91

Browse files
Adds 'date_format' parameter to 'read_csv' (#650)
* Added test for date_format in read_csv * Added date_format to read_csv * date_format int col mapping and extra tests --------- Co-authored-by: Sjors Lockhorst <[email protected]>
1 parent b9895fd commit a28df91

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pandas-stubs/io/parsers/readers.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def read_csv(
6767
infer_datetime_format: bool = ...,
6868
keep_date_col: bool = ...,
6969
date_parser: Callable = ...,
70+
date_format: str | Mapping[int | str, str] | None = ...,
7071
dayfirst: bool = ...,
7172
cache_dates: bool = ...,
7273
iterator: Literal[True],
@@ -126,6 +127,7 @@ def read_csv(
126127
infer_datetime_format: bool = ...,
127128
keep_date_col: bool = ...,
128129
date_parser: Callable = ...,
130+
date_format: str | Mapping[int | str, str] | None = ...,
129131
dayfirst: bool = ...,
130132
cache_dates: bool = ...,
131133
iterator: bool = ...,
@@ -185,6 +187,7 @@ def read_csv(
185187
infer_datetime_format: bool = ...,
186188
keep_date_col: bool = ...,
187189
date_parser: Callable = ...,
190+
date_format: str | Mapping[int | str, str] | None = ...,
188191
dayfirst: bool = ...,
189192
cache_dates: bool = ...,
190193
iterator: Literal[False] = ...,

tests/test_io.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,21 @@ def test_types_read_csv() -> None:
638638
tfr4: TextFileReader = pd.read_csv(path, nrows=2, iterator=True)
639639
tfr4.close()
640640

641+
df_dates = pd.DataFrame(data={"col1": ["2023-03-15", "2023-04-20"]})
642+
643+
with ensure_clean() as path:
644+
df_dates.to_csv(path)
645+
646+
df26: pd.DataFrame = pd.read_csv(
647+
path, parse_dates=["col1"], date_format="%Y-%m-%d"
648+
)
649+
df27: pd.DataFrame = pd.read_csv(
650+
path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"}
651+
)
652+
df28: pd.DataFrame = pd.read_csv(
653+
path, parse_dates=["col1"], date_format={1: "%Y-%m-%d"}
654+
)
655+
641656

642657
def test_read_table():
643658
with ensure_clean() as path:

0 commit comments

Comments
 (0)