diff --git a/pandas-stubs/io/excel/_base.pyi b/pandas-stubs/io/excel/_base.pyi index 68425625..c5638a68 100644 --- a/pandas-stubs/io/excel/_base.pyi +++ b/pandas-stubs/io/excel/_base.pyi @@ -62,7 +62,7 @@ def read_excel( | Sequence[int] | Sequence[Sequence[str] | Sequence[int]] | dict[str, Sequence[int] | list[str]] = ..., - date_parser: Callable | None = ..., + date_format: dict[Hashable, str] | str | None = ..., thousands: str | None = ..., decimal: str = ..., comment: str | None = ..., @@ -101,7 +101,7 @@ def read_excel( | Sequence[int] | Sequence[Sequence[str] | Sequence[int]] | dict[str, Sequence[int] | list[str]] = ..., - date_parser: Callable | None = ..., + date_format: dict[Hashable, str] | str | None = ..., thousands: str | None = ..., decimal: str = ..., comment: str | None = ..., diff --git a/pandas-stubs/io/parsers/readers.pyi b/pandas-stubs/io/parsers/readers.pyi index 81c20dcc..e25a96be 100644 --- a/pandas-stubs/io/parsers/readers.pyi +++ b/pandas-stubs/io/parsers/readers.pyi @@ -4,6 +4,7 @@ from collections import ( ) from collections.abc import ( Callable, + Hashable, Mapping, Sequence, ) @@ -68,8 +69,7 @@ def read_csv( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., - date_format: str | Mapping[int | str, str] | None = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: Literal[True], @@ -129,8 +129,7 @@ def read_csv( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., - date_format: str | Mapping[int | str, str] | None = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: bool = ..., @@ -190,8 +189,7 @@ def read_csv( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., - date_format: str | Mapping[int | str, str] | None = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: Literal[False] = ..., @@ -251,7 +249,7 @@ def read_table( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: Literal[True], @@ -310,7 +308,7 @@ def read_table( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: bool = ..., @@ -369,7 +367,7 @@ def read_table( | Mapping[str, Sequence[int | str]] = ..., infer_datetime_format: bool = ..., keep_date_col: bool = ..., - date_parser: Callable = ..., + date_format: dict[Hashable, str] | str | None = ..., dayfirst: bool = ..., cache_dates: bool = ..., iterator: Literal[False] = ..., @@ -402,6 +400,7 @@ def read_fwf( widths: Sequence[int] | None = ..., infer_nrows: int = ..., dtype_backend: DtypeBackend | NoDefault = ..., + date_format: dict[Hashable, str] | str | None = ..., iterator: Literal[True], chunksize: int | None = ..., **kwds: Any, @@ -414,6 +413,7 @@ def read_fwf( widths: Sequence[int] | None = ..., infer_nrows: int = ..., dtype_backend: DtypeBackend | NoDefault = ..., + date_format: dict[Hashable, str] | str | None = ..., iterator: bool = ..., chunksize: int, **kwds: Any, @@ -426,6 +426,7 @@ def read_fwf( widths: Sequence[int] | None = ..., infer_nrows: int = ..., dtype_backend: DtypeBackend | NoDefault = ..., + date_format: dict[Hashable, str] | str | None = ..., iterator: Literal[False] = ..., chunksize: None = ..., **kwds: Any, diff --git a/tests/test_io.py b/tests/test_io.py index ac529eae..8044a79c 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1405,3 +1405,101 @@ def test_read_sql_dict_str_value_dtype() -> None: DataFrame, ) con.close() + + +def test_added_date_format() -> None: + with ensure_clean() as path: + df_dates = pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ) + df_dates.to_csv(path) + + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d" + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_table( + path, + sep=",", + parse_dates=["col1"], + date_format={"col1": "%Y-%m-%d"}, + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_table( + path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"} + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + + check( + assert_type( + pd.read_fwf(path, date_format="%Y-%m-%d"), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_fwf(path, date_format={0: "%Y-%m-%d"}), + pd.DataFrame, + ), + pd.DataFrame, + ) + with ensure_clean(".xlsx") as path: + check( + assert_type( + pd.DataFrame( + data={ + "col1": ["2023-03-15", "2023-04-20"], + } + ).to_excel(path), + None, + ), + type(None), + ) + check( + assert_type( + pd.read_excel(path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"}), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel( + path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"} + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"), + pd.DataFrame, + ), + pd.DataFrame, + )