|
1 | 1 | from datetime import datetime, timedelta
|
2 | 2 | import inspect
|
3 | 3 | import re
|
4 |
| -from typing import TYPE_CHECKING, Any, List, Optional |
| 4 | +from typing import TYPE_CHECKING, Any, List, Optional, Type, Union, cast |
5 | 5 | import warnings
|
6 | 6 |
|
7 | 7 | import numpy as np
|
@@ -93,6 +93,8 @@ class Block(PandasObject):
|
93 | 93 | Index-ignorant; let the container take care of that
|
94 | 94 | """
|
95 | 95 |
|
| 96 | + values: Union[np.ndarray, ExtensionArray] |
| 97 | + |
96 | 98 | __slots__ = ["_mgr_locs", "values", "ndim"]
|
97 | 99 | is_numeric = False
|
98 | 100 | is_float = False
|
@@ -194,7 +196,9 @@ def _consolidate_key(self):
|
194 | 196 | @property
|
195 | 197 | def is_view(self) -> bool:
|
196 | 198 | """ return a boolean if I am possibly a view """
|
197 |
| - return self.values.base is not None |
| 199 | + values = self.values |
| 200 | + values = cast(np.ndarray, values) |
| 201 | + return values.base is not None |
198 | 202 |
|
199 | 203 | @property
|
200 | 204 | def is_categorical(self) -> bool:
|
@@ -1465,6 +1469,7 @@ def where_func(cond, values, other):
|
1465 | 1469 | result_blocks: List["Block"] = []
|
1466 | 1470 | for m in [mask, ~mask]:
|
1467 | 1471 | if m.any():
|
| 1472 | + result = cast(np.ndarray, result) # EABlock overrides where |
1468 | 1473 | taken = result.take(m.nonzero()[0], axis=axis)
|
1469 | 1474 | r = maybe_downcast_numeric(taken, self.dtype)
|
1470 | 1475 | nb = self.make_block(r.T, placement=self.mgr_locs[m])
|
@@ -1619,6 +1624,8 @@ class ExtensionBlock(Block):
|
1619 | 1624 | _validate_ndim = False
|
1620 | 1625 | is_extension = True
|
1621 | 1626 |
|
| 1627 | + values: ExtensionArray |
| 1628 | + |
1622 | 1629 | def __init__(self, values, placement, ndim=None):
|
1623 | 1630 | """
|
1624 | 1631 | Initialize a non-consolidatable block.
|
@@ -2197,9 +2204,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
|
2197 | 2204 | if copy:
|
2198 | 2205 | # this should be the only copy
|
2199 | 2206 | values = values.copy()
|
2200 |
| - if getattr(values, "tz", None) is None: |
2201 |
| - values = DatetimeArray(values).tz_localize("UTC") |
2202 |
| - values = values.tz_convert(dtype.tz) |
| 2207 | + values = DatetimeArray._simple_new(values.view("i8"), dtype=dtype) |
2203 | 2208 | return self.make_block(values)
|
2204 | 2209 |
|
2205 | 2210 | # delegate
|
@@ -2243,6 +2248,8 @@ def set(self, locs, values):
|
2243 | 2248 | class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
|
2244 | 2249 | """ implement a datetime64 block with a tz attribute """
|
2245 | 2250 |
|
| 2251 | + values: DatetimeArray |
| 2252 | + |
2246 | 2253 | __slots__ = ()
|
2247 | 2254 | is_datetimetz = True
|
2248 | 2255 | is_extension = True
|
@@ -2670,6 +2677,8 @@ def get_block_type(values, dtype=None):
|
2670 | 2677 | dtype = dtype or values.dtype
|
2671 | 2678 | vtype = dtype.type
|
2672 | 2679 |
|
| 2680 | + cls: Type[Block] |
| 2681 | + |
2673 | 2682 | if is_sparse(dtype):
|
2674 | 2683 | # Need this first(ish) so that Sparse[datetime] is sparse
|
2675 | 2684 | cls = ExtensionBlock
|
|
0 commit comments