|
1 | 1 | import json |
2 | 2 | from functools import lru_cache |
3 | 3 | from pathlib import Path |
4 | | -from typing import TYPE_CHECKING, Union, Callable, Dict, Optional |
| 4 | +from typing import TYPE_CHECKING, Any, Union, Callable, Optional |
5 | 5 |
|
6 | 6 | if TYPE_CHECKING: |
7 | 7 | import pandas as pd |
8 | 8 | from azure.kusto.data._models import KustoResultTable, KustoStreamingResultTable |
9 | 9 |
|
10 | 10 | # Alias for dataframe_from_result_table converter type |
11 | | -Converter = Dict[str, Union[str, Callable[[str, "pd.DataFrame"], "pd.Series"]]] |
| 11 | +Converter = dict[str, Union[str, Callable[[str, "pd.DataFrame"], "pd.Series['Any']"]]] |
12 | 12 |
|
13 | 13 |
|
14 | | -def load_bundled_json(file_name: str) -> Dict: |
| 14 | +def load_bundled_json(file_name: str) -> dict[Any, Any]: |
15 | 15 | filename = Path(__file__).absolute().parent.joinpath(file_name) |
16 | 16 | with filename.open("r", encoding="utf-8") as data: |
17 | 17 | return json.load(data) |
@@ -118,18 +118,18 @@ def parse_float(frame, col): |
118 | 118 | return frame[col] |
119 | 119 |
|
120 | 120 |
|
121 | | -def parse_datetime(frame, col): |
| 121 | +def parse_datetime(frame, col, force_version: Optional[str] = None): |
122 | 122 | # Pandas before version 2 doesn't support the "format" arg |
123 | 123 | import pandas as pd |
124 | 124 |
|
125 | 125 | args = {} |
126 | | - if pd.__version__.startswith("2."): |
| 126 | + if (force_version or pd.__version__).startswith("2."): |
127 | 127 | args = {"format": "ISO8601", "utc": True} |
128 | 128 | else: |
129 | 129 | # if frame contains ".", replace "Z" with ".000Z" |
130 | 130 | # == False is not a mistake - that's the pandas way to do it |
131 | | - contains_dot = frame[col].str.contains(".") |
132 | | - frame.loc[not contains_dot, col] = frame.loc[not contains_dot, col].str.replace("Z", ".000Z") |
| 131 | + contains_dot = frame[col].str.contains("\\.") |
| 132 | + frame.loc[~contains_dot, col] = frame.loc[~contains_dot, col].str.replace("Z", ".000Z") |
133 | 133 | frame[col] = pd.to_datetime(frame[col], errors="coerce", **args) |
134 | 134 | return frame[col] |
135 | 135 |
|
|
0 commit comments