17
17
18
18
from pandas ._libs import (
19
19
Interval ,
20
- NaT ,
21
20
Period ,
22
21
Timestamp ,
23
22
algos as libalgos ,
45
44
maybe_downcast_numeric ,
46
45
maybe_downcast_to_dtype ,
47
46
maybe_upcast ,
47
+ sanitize_to_nanoseconds ,
48
48
soft_convert_objects ,
49
49
)
50
50
from pandas .core .dtypes .common import (
72
72
from pandas .core .dtypes .missing import (
73
73
is_valid_na_for_dtype ,
74
74
isna ,
75
+ na_value_for_dtype ,
75
76
)
76
77
77
78
import pandas .core .algorithms as algos
95
96
DatetimeArray ,
96
97
ExtensionArray ,
97
98
PandasArray ,
98
- TimedeltaArray ,
99
99
)
100
100
from pandas .core .base import PandasObject
101
101
import pandas .core .common as com
102
- from pandas .core .construction import extract_array
102
+ from pandas .core .construction import (
103
+ ensure_wrapped_if_datetimelike ,
104
+ extract_array ,
105
+ )
103
106
from pandas .core .indexers import (
104
107
check_setitem_lengths ,
105
108
is_empty_indexer ,
@@ -2095,8 +2098,6 @@ class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
2095
2098
2096
2099
is_numeric = False
2097
2100
_can_hold_na = True
2098
- _dtype : np .dtype
2099
- _holder : Type [Union [DatetimeArray , TimedeltaArray ]]
2100
2101
2101
2102
@classmethod
2102
2103
def _maybe_coerce_values (cls , values ):
@@ -2112,25 +2113,26 @@ def _maybe_coerce_values(cls, values):
2112
2113
Returns
2113
2114
-------
2114
2115
values : ndarray[datetime64ns/timedelta64ns]
2115
-
2116
- Overridden by DatetimeTZBlock.
2117
2116
"""
2118
- if values .dtype != cls ._dtype :
2119
- # non-nano we will convert to nano
2120
- if values .dtype .kind != cls ._dtype .kind :
2121
- # caller is responsible for ensuring td64/dt64 dtype
2122
- raise TypeError (values .dtype ) # pragma: no cover
2123
-
2124
- values = cls ._holder ._from_sequence (values )._data
2125
-
2126
- if isinstance (values , cls ._holder ):
2117
+ values = extract_array (values , extract_numpy = True )
2118
+ if isinstance (values , np .ndarray ):
2119
+ values = sanitize_to_nanoseconds (values )
2120
+ elif isinstance (values .dtype , np .dtype ):
2121
+ # i.e. not datetime64tz
2127
2122
values = values ._data
2128
2123
2129
- assert isinstance (values , np .ndarray ), type (values )
2130
2124
return values
2131
2125
2132
2126
def array_values (self ):
2133
- return self ._holder ._simple_new (self .values )
2127
+ return ensure_wrapped_if_datetimelike (self .values )
2128
+
2129
+ @property
2130
+ def _holder (self ):
2131
+ return type (self .array_values ())
2132
+
2133
+ @property
2134
+ def fill_value (self ):
2135
+ return na_value_for_dtype (self .dtype )
2134
2136
2135
2137
def to_native_types (self , na_rep = "NaT" , ** kwargs ):
2136
2138
""" convert to our native types format """
@@ -2142,9 +2144,6 @@ def to_native_types(self, na_rep="NaT", **kwargs):
2142
2144
2143
2145
class DatetimeBlock (DatetimeLikeBlockMixin ):
2144
2146
__slots__ = ()
2145
- fill_value = np .datetime64 ("NaT" , "ns" )
2146
- _dtype = fill_value .dtype
2147
- _holder = DatetimeArray
2148
2147
2149
2148
def set_inplace (self , locs , values ):
2150
2149
"""
@@ -2165,42 +2164,16 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
2165
2164
_can_hold_na = True
2166
2165
is_numeric = False
2167
2166
2168
- _holder = DatetimeArray
2169
-
2170
2167
internal_values = Block .internal_values
2171
2168
_can_hold_element = DatetimeBlock ._can_hold_element
2172
2169
to_native_types = DatetimeBlock .to_native_types
2173
2170
diff = DatetimeBlock .diff
2174
- fill_value = NaT
2175
2171
where = DatetimeBlock .where
2176
2172
putmask = DatetimeLikeBlockMixin .putmask
2177
2173
fillna = DatetimeLikeBlockMixin .fillna
2178
2174
2179
2175
array_values = ExtensionBlock .array_values
2180
2176
2181
- @classmethod
2182
- def _maybe_coerce_values (cls , values ):
2183
- """
2184
- Input validation for values passed to __init__. Ensure that
2185
- we have datetime64TZ, coercing if necessary.
2186
-
2187
- Parameters
2188
- ----------
2189
- values : array-like
2190
- Must be convertible to datetime64
2191
-
2192
- Returns
2193
- -------
2194
- values : DatetimeArray
2195
- """
2196
- if not isinstance (values , cls ._holder ):
2197
- values = cls ._holder (values )
2198
-
2199
- if values .tz is None :
2200
- raise ValueError ("cannot create a DatetimeTZBlock without a tz" )
2201
-
2202
- return values
2203
-
2204
2177
@property
2205
2178
def is_view (self ) -> bool :
2206
2179
""" return a boolean if I am possibly a view """
@@ -2216,9 +2189,6 @@ def external_values(self):
2216
2189
2217
2190
class TimeDeltaBlock (DatetimeLikeBlockMixin ):
2218
2191
__slots__ = ()
2219
- _holder = TimedeltaArray
2220
- fill_value = np .timedelta64 ("NaT" , "ns" )
2221
- _dtype = fill_value .dtype
2222
2192
2223
2193
2224
2194
class ObjectBlock (Block ):
0 commit comments