Skip to content

Commit 660c4b0

Browse files
ramvikramsDr-Irv
andauthored
GH:624- added date_format to read_table,read_fwf and read_excel (#695)
* GH-676 * Update test_io.py * Update tests/test_io.py Co-authored-by: Irv Lustig <[email protected]> * 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 <[email protected]>
1 parent a81424d commit 660c4b0

File tree

3 files changed

+110
-11
lines changed

3 files changed

+110
-11
lines changed

pandas-stubs/io/excel/_base.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def read_excel(
6262
| Sequence[int]
6363
| Sequence[Sequence[str] | Sequence[int]]
6464
| dict[str, Sequence[int] | list[str]] = ...,
65-
date_parser: Callable | None = ...,
65+
date_format: dict[Hashable, str] | str | None = ...,
6666
thousands: str | None = ...,
6767
decimal: str = ...,
6868
comment: str | None = ...,
@@ -101,7 +101,7 @@ def read_excel(
101101
| Sequence[int]
102102
| Sequence[Sequence[str] | Sequence[int]]
103103
| dict[str, Sequence[int] | list[str]] = ...,
104-
date_parser: Callable | None = ...,
104+
date_format: dict[Hashable, str] | str | None = ...,
105105
thousands: str | None = ...,
106106
decimal: str = ...,
107107
comment: str | None = ...,

pandas-stubs/io/parsers/readers.pyi

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from collections import (
44
)
55
from collections.abc import (
66
Callable,
7+
Hashable,
78
Mapping,
89
Sequence,
910
)
@@ -68,8 +69,7 @@ def read_csv(
6869
| Mapping[str, Sequence[int | str]] = ...,
6970
infer_datetime_format: bool = ...,
7071
keep_date_col: bool = ...,
71-
date_parser: Callable = ...,
72-
date_format: str | Mapping[int | str, str] | None = ...,
72+
date_format: dict[Hashable, str] | str | None = ...,
7373
dayfirst: bool = ...,
7474
cache_dates: bool = ...,
7575
iterator: Literal[True],
@@ -129,8 +129,7 @@ def read_csv(
129129
| Mapping[str, Sequence[int | str]] = ...,
130130
infer_datetime_format: bool = ...,
131131
keep_date_col: bool = ...,
132-
date_parser: Callable = ...,
133-
date_format: str | Mapping[int | str, str] | None = ...,
132+
date_format: dict[Hashable, str] | str | None = ...,
134133
dayfirst: bool = ...,
135134
cache_dates: bool = ...,
136135
iterator: bool = ...,
@@ -190,8 +189,7 @@ def read_csv(
190189
| Mapping[str, Sequence[int | str]] = ...,
191190
infer_datetime_format: bool = ...,
192191
keep_date_col: bool = ...,
193-
date_parser: Callable = ...,
194-
date_format: str | Mapping[int | str, str] | None = ...,
192+
date_format: dict[Hashable, str] | str | None = ...,
195193
dayfirst: bool = ...,
196194
cache_dates: bool = ...,
197195
iterator: Literal[False] = ...,
@@ -251,7 +249,7 @@ def read_table(
251249
| Mapping[str, Sequence[int | str]] = ...,
252250
infer_datetime_format: bool = ...,
253251
keep_date_col: bool = ...,
254-
date_parser: Callable = ...,
252+
date_format: dict[Hashable, str] | str | None = ...,
255253
dayfirst: bool = ...,
256254
cache_dates: bool = ...,
257255
iterator: Literal[True],
@@ -310,7 +308,7 @@ def read_table(
310308
| Mapping[str, Sequence[int | str]] = ...,
311309
infer_datetime_format: bool = ...,
312310
keep_date_col: bool = ...,
313-
date_parser: Callable = ...,
311+
date_format: dict[Hashable, str] | str | None = ...,
314312
dayfirst: bool = ...,
315313
cache_dates: bool = ...,
316314
iterator: bool = ...,
@@ -369,7 +367,7 @@ def read_table(
369367
| Mapping[str, Sequence[int | str]] = ...,
370368
infer_datetime_format: bool = ...,
371369
keep_date_col: bool = ...,
372-
date_parser: Callable = ...,
370+
date_format: dict[Hashable, str] | str | None = ...,
373371
dayfirst: bool = ...,
374372
cache_dates: bool = ...,
375373
iterator: Literal[False] = ...,
@@ -402,6 +400,7 @@ def read_fwf(
402400
widths: Sequence[int] | None = ...,
403401
infer_nrows: int = ...,
404402
dtype_backend: DtypeBackend | NoDefault = ...,
403+
date_format: dict[Hashable, str] | str | None = ...,
405404
iterator: Literal[True],
406405
chunksize: int | None = ...,
407406
**kwds: Any,
@@ -414,6 +413,7 @@ def read_fwf(
414413
widths: Sequence[int] | None = ...,
415414
infer_nrows: int = ...,
416415
dtype_backend: DtypeBackend | NoDefault = ...,
416+
date_format: dict[Hashable, str] | str | None = ...,
417417
iterator: bool = ...,
418418
chunksize: int,
419419
**kwds: Any,
@@ -426,6 +426,7 @@ def read_fwf(
426426
widths: Sequence[int] | None = ...,
427427
infer_nrows: int = ...,
428428
dtype_backend: DtypeBackend | NoDefault = ...,
429+
date_format: dict[Hashable, str] | str | None = ...,
429430
iterator: Literal[False] = ...,
430431
chunksize: None = ...,
431432
**kwds: Any,

tests/test_io.py

+98
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,101 @@ def test_read_sql_dict_str_value_dtype() -> None:
14051405
DataFrame,
14061406
)
14071407
con.close()
1408+
1409+
1410+
def test_added_date_format() -> None:
1411+
with ensure_clean() as path:
1412+
df_dates = pd.DataFrame(
1413+
data={
1414+
"col1": ["2023-03-15", "2023-04-20"],
1415+
}
1416+
)
1417+
df_dates.to_csv(path)
1418+
1419+
check(
1420+
assert_type(
1421+
pd.read_table(
1422+
path, sep=",", parse_dates=["col1"], date_format="%Y-%m-%d"
1423+
),
1424+
pd.DataFrame,
1425+
),
1426+
pd.DataFrame,
1427+
)
1428+
check(
1429+
assert_type(
1430+
pd.read_table(
1431+
path,
1432+
sep=",",
1433+
parse_dates=["col1"],
1434+
date_format={"col1": "%Y-%m-%d"},
1435+
),
1436+
pd.DataFrame,
1437+
),
1438+
pd.DataFrame,
1439+
)
1440+
check(
1441+
assert_type(
1442+
pd.read_table(
1443+
path, sep=",", parse_dates=["col1"], date_format={0: "%Y-%m-%d"}
1444+
),
1445+
pd.DataFrame,
1446+
),
1447+
pd.DataFrame,
1448+
)
1449+
1450+
check(
1451+
assert_type(
1452+
pd.read_fwf(path, date_format="%Y-%m-%d"),
1453+
pd.DataFrame,
1454+
),
1455+
pd.DataFrame,
1456+
)
1457+
check(
1458+
assert_type(
1459+
pd.read_fwf(path, date_format={"col1": "%Y-%m-%d"}),
1460+
pd.DataFrame,
1461+
),
1462+
pd.DataFrame,
1463+
)
1464+
check(
1465+
assert_type(
1466+
pd.read_fwf(path, date_format={0: "%Y-%m-%d"}),
1467+
pd.DataFrame,
1468+
),
1469+
pd.DataFrame,
1470+
)
1471+
with ensure_clean(".xlsx") as path:
1472+
check(
1473+
assert_type(
1474+
pd.DataFrame(
1475+
data={
1476+
"col1": ["2023-03-15", "2023-04-20"],
1477+
}
1478+
).to_excel(path),
1479+
None,
1480+
),
1481+
type(None),
1482+
)
1483+
check(
1484+
assert_type(
1485+
pd.read_excel(path, parse_dates=["col1"], date_format={0: "%Y-%m-%d"}),
1486+
pd.DataFrame,
1487+
),
1488+
pd.DataFrame,
1489+
)
1490+
check(
1491+
assert_type(
1492+
pd.read_excel(
1493+
path, parse_dates=["col1"], date_format={"col1": "%Y-%m-%d"}
1494+
),
1495+
pd.DataFrame,
1496+
),
1497+
pd.DataFrame,
1498+
)
1499+
check(
1500+
assert_type(
1501+
pd.read_excel(path, parse_dates=["col1"], date_format="%Y-%m-%d"),
1502+
pd.DataFrame,
1503+
),
1504+
pd.DataFrame,
1505+
)

0 commit comments

Comments
 (0)