Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH:624- added date_format to read_table,read_fwf and read_excel #695

Merged
merged 10 commits into from
May 18, 2023
4 changes: 2 additions & 2 deletions pandas-stubs/io/excel/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
13 changes: 7 additions & 6 deletions pandas-stubs/io/parsers/readers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from collections import (
)
from collections.abc import (
Callable,
Hashable,
Mapping,
Sequence,
)
Expand Down Expand Up @@ -68,7 +69,6 @@ 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 = ...,
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
dayfirst: bool = ...,
cache_dates: bool = ...,
Expand Down Expand Up @@ -129,7 +129,6 @@ 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 = ...,
dayfirst: bool = ...,
cache_dates: bool = ...,
Expand Down Expand Up @@ -190,7 +189,6 @@ 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 = ...,
dayfirst: bool = ...,
cache_dates: bool = ...,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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: str | None = ...,
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
dayfirst: bool = ...,
cache_dates: bool = ...,
iterator: Literal[False] = ...,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
91 changes: 91 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,3 +1405,94 @@ def test_read_sql_dict_str_value_dtype() -> None:
DataFrame,
)
con.close()


def test_added_date_format() -> None:
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
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_csv(path, parse_dates=["col1"], date_format="%Y-%m-%d"),
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
pd.read_csv(
path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"}
),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
pd.read_csv(path, parse_dates=["col1"], date_format={10: "%Y-%m-%d"}),
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
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={10: "%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={10: "%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,
)