|
1 | 1 | import io |
2 | 2 | from itertools import cycle |
3 | | -from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Union |
| 3 | +from typing import ( |
| 4 | + Any, |
| 5 | + Callable, |
| 6 | + Dict, |
| 7 | + List, |
| 8 | + Mapping, |
| 9 | + Optional, |
| 10 | + Sequence, |
| 11 | + Tuple, |
| 12 | + Union, |
| 13 | + Hashable, |
| 14 | + cast, |
| 15 | +) |
4 | 16 |
|
5 | 17 | import ipyleaflet # type: ignore |
6 | 18 | import numpy as np |
@@ -39,7 +51,7 @@ def __init__( |
39 | 51 | # data resources, and so column names and dtype need to be |
40 | 52 | # passed in as parameters. |
41 | 53 | self._aim_metadata_columns: Optional[List[str]] = None |
42 | | - self._aim_metadata_dtype: Dict[str, Any] = dict() |
| 54 | + self._aim_metadata_dtype: Dict[str, Union[str, type, np.dtype]] = dict() |
43 | 55 | if isinstance(aim_metadata_dtype, Mapping): |
44 | 56 | self._aim_metadata_columns = list(aim_metadata_dtype.keys()) |
45 | 57 | self._aim_metadata_dtype.update(aim_metadata_dtype) |
@@ -140,7 +152,19 @@ def _parse_general_metadata( |
140 | 152 | "longitude": "float64", |
141 | 153 | "sex_call": "object", |
142 | 154 | } |
143 | | - df = pd.read_csv(io.BytesIO(data), dtype=dtype, na_values="") |
| 155 | + # Mapping of string dtypes to actual dtypes |
| 156 | + dtype_map = { |
| 157 | + "object": str, |
| 158 | + "int64": np.int64, |
| 159 | + "float64": np.float64, |
| 160 | + } |
| 161 | + |
| 162 | + # Convert string dtypes to actual dtypes |
| 163 | + dtype_fixed: Mapping[Hashable, Union[str, np.dtype, type]] = { |
| 164 | + col: dtype_map.get(dtype[col], str) for col in dtype |
| 165 | + } |
| 166 | + |
| 167 | + df = pd.read_csv(io.BytesIO(data), dtype=dtype_fixed, na_values="") |
144 | 168 |
|
145 | 169 | # Ensure all column names are lower case. |
146 | 170 | df.columns = [c.lower() for c in df.columns] # type: ignore |
@@ -460,7 +484,12 @@ def _parse_aim_metadata( |
460 | 484 | if isinstance(data, bytes): |
461 | 485 | # Parse CSV data. |
462 | 486 | df = pd.read_csv( |
463 | | - io.BytesIO(data), dtype=self._aim_metadata_dtype, na_values="" |
| 487 | + io.BytesIO(data), |
| 488 | + dtype=cast( |
| 489 | + Mapping[Hashable, Union[str, type, np.dtype]], |
| 490 | + self._aim_metadata_dtype, |
| 491 | + ), |
| 492 | + na_values="", |
464 | 493 | ) |
465 | 494 |
|
466 | 495 | # Ensure all column names are lower case. |
|
0 commit comments