Skip to content

Commit

Permalink
GH:624- added date_format to read_table,read_fwf and `read_exce…
Browse files Browse the repository at this point in the history
…l` (#695)

* GH-676

* Update test_io.py

* Update tests/test_io.py

Co-authored-by: Irv Lustig <irv@princeton.com>

* Update test_io.py

* added `date_format` to `read_table`,`read_fwf` and `read_excel`

* req changes

* Update test_io.py

* Update test_io.py

* made `date_format` consistent and changed the tests

---------

Co-authored-by: Irv Lustig <irv@princeton.com>
  • Loading branch information
ramvikrams and Dr-Irv authored May 18, 2023
1 parent a81424d commit 660c4b0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
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
19 changes: 10 additions & 9 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,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],
Expand Down Expand Up @@ -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 = ...,
Expand Down Expand Up @@ -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] = ...,
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: dict[Hashable, str] | str | None = ...,
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
98 changes: 98 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 660c4b0

Please sign in to comment.