1
1
from __future__ import annotations
2
2
3
+ from typing import (
4
+ TYPE_CHECKING ,
5
+ cast ,
6
+ )
7
+
3
8
import numpy as np
4
9
5
10
from pandas ._typing import (
16
21
17
22
from pandas .io .excel ._base import BaseExcelReader
18
23
24
+ if TYPE_CHECKING :
25
+ from pandas ._libs .tslibs .nattype import NaTType
26
+
19
27
20
28
@doc (storage_options = _shared_docs ["storage_options" ])
21
29
class ODFReader (BaseExcelReader ):
@@ -81,7 +89,9 @@ def get_sheet_by_name(self, name: str):
81
89
self .close ()
82
90
raise ValueError (f"sheet { name } not found" )
83
91
84
- def get_sheet_data (self , sheet , convert_float : bool ) -> list [list [Scalar ]]:
92
+ def get_sheet_data (
93
+ self , sheet , convert_float : bool
94
+ ) -> list [list [Scalar | NaTType ]]:
85
95
"""
86
96
Parse an ODF Table into a list of lists
87
97
"""
@@ -99,12 +109,12 @@ def get_sheet_data(self, sheet, convert_float: bool) -> list[list[Scalar]]:
99
109
empty_rows = 0
100
110
max_row_len = 0
101
111
102
- table : list [list [Scalar ]] = []
112
+ table : list [list [Scalar | NaTType ]] = []
103
113
104
114
for sheet_row in sheet_rows :
105
115
sheet_cells = [x for x in sheet_row .childNodes if x .qname in cell_names ]
106
116
empty_cells = 0
107
- table_row : list [Scalar ] = []
117
+ table_row : list [Scalar | NaTType ] = []
108
118
109
119
for sheet_cell in sheet_cells :
110
120
if sheet_cell .qname == table_cell_name :
@@ -167,7 +177,7 @@ def _is_empty_row(self, row) -> bool:
167
177
168
178
return True
169
179
170
- def _get_cell_value (self , cell , convert_float : bool ) -> Scalar :
180
+ def _get_cell_value (self , cell , convert_float : bool ) -> Scalar | NaTType :
171
181
from odf .namespaces import OFFICENS
172
182
173
183
if str (cell ) == "#N/A" :
@@ -200,9 +210,11 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
200
210
cell_value = cell .attributes .get ((OFFICENS , "date-value" ))
201
211
return pd .to_datetime (cell_value )
202
212
elif cell_type == "time" :
203
- stamp = pd .to_datetime (str (cell ))
204
- # error: Item "str" of "Union[float, str, NaTType]" has no attribute "time"
205
- return stamp .time () # type: ignore[union-attr]
213
+ # cast needed because `pd.to_datetime can return NaTType,
214
+ # but we know this is a valid time
215
+ stamp = cast (pd .Timestamp , pd .to_datetime (str (cell )))
216
+ # cast needed here because Scalar doesn't include datetime.time
217
+ return cast (Scalar , stamp .time ())
206
218
else :
207
219
self .close ()
208
220
raise ValueError (f"Unrecognized type { cell_type } " )
0 commit comments