76
76
from collections .abc import Iterator
77
77
78
78
from pandas ._typing import (
79
+ ArrayLike ,
79
80
DateTimeErrorChoices ,
80
81
DtypeObj ,
81
82
IntervalClosedType ,
@@ -327,13 +328,10 @@ def _from_sequence_not_strict(
327
328
dayfirst : bool = False ,
328
329
yearfirst : bool = False ,
329
330
ambiguous : TimeAmbiguous = "raise" ,
330
- ):
331
+ ) -> Self :
331
332
"""
332
333
A non-strict version of _from_sequence, called from DatetimeIndex.__new__.
333
334
"""
334
- explicit_none = freq is None
335
- freq = freq if freq is not lib .no_default else None
336
- freq , freq_infer = dtl .maybe_infer_freq (freq )
337
335
338
336
# if the user either explicitly passes tz=None or a tz-naive dtype, we
339
337
# disallows inferring a tz.
@@ -349,13 +347,16 @@ def _from_sequence_not_strict(
349
347
350
348
unit = None
351
349
if dtype is not None :
352
- if isinstance (dtype , np .dtype ):
353
- unit = np .datetime_data (dtype )[0 ]
354
- else :
355
- # DatetimeTZDtype
356
- unit = dtype .unit
350
+ unit = dtl .dtype_to_unit (dtype )
351
+
352
+ data , copy = dtl .ensure_arraylike_for_datetimelike (
353
+ data , copy , cls_name = "DatetimeArray"
354
+ )
355
+ inferred_freq = None
356
+ if isinstance (data , DatetimeArray ):
357
+ inferred_freq = data .freq
357
358
358
- subarr , tz , inferred_freq = _sequence_to_dt64 (
359
+ subarr , tz = _sequence_to_dt64 (
359
360
data ,
360
361
copy = copy ,
361
362
tz = tz ,
@@ -372,26 +373,15 @@ def _from_sequence_not_strict(
372
373
"Use obj.tz_localize(None) instead."
373
374
)
374
375
375
- freq , freq_infer = dtl .validate_inferred_freq (freq , inferred_freq , freq_infer )
376
- if explicit_none :
377
- freq = None
378
-
379
376
data_unit = np .datetime_data (subarr .dtype )[0 ]
380
377
data_dtype = tz_to_dtype (tz , data_unit )
381
- result = cls ._simple_new (subarr , freq = freq , dtype = data_dtype )
378
+ result = cls ._simple_new (subarr , freq = inferred_freq , dtype = data_dtype )
382
379
if unit is not None and unit != result .unit :
383
380
# If unit was specified in user-passed dtype, cast to it here
384
381
result = result .as_unit (unit )
385
382
386
- if inferred_freq is None and freq is not None :
387
- # this condition precludes `freq_infer`
388
- cls ._validate_frequency (result , freq , ambiguous = ambiguous )
389
-
390
- elif freq_infer :
391
- # Set _freq directly to bypass duplicative _validate_frequency
392
- # check.
393
- result ._freq = to_offset (result .inferred_freq )
394
-
383
+ validate_kwds = {"ambiguous" : ambiguous }
384
+ result ._maybe_pin_freq (freq , validate_kwds )
395
385
return result
396
386
397
387
# error: Signature of "_generate_range" incompatible with supertype
@@ -2180,7 +2170,7 @@ def std(
2180
2170
2181
2171
2182
2172
def _sequence_to_dt64 (
2183
- data ,
2173
+ data : ArrayLike ,
2184
2174
* ,
2185
2175
copy : bool = False ,
2186
2176
tz : tzinfo | None = None ,
@@ -2192,7 +2182,8 @@ def _sequence_to_dt64(
2192
2182
"""
2193
2183
Parameters
2194
2184
----------
2195
- data : list-like
2185
+ data : np.ndarray or ExtensionArray
2186
+ dtl.ensure_arraylike_for_datetimelike has already been called.
2196
2187
copy : bool, default False
2197
2188
tz : tzinfo or None, default None
2198
2189
dayfirst : bool, default False
@@ -2209,21 +2200,11 @@ def _sequence_to_dt64(
2209
2200
Where `unit` is "ns" unless specified otherwise by `out_unit`.
2210
2201
tz : tzinfo or None
2211
2202
Either the user-provided tzinfo or one inferred from the data.
2212
- inferred_freq : Tick or None
2213
- The inferred frequency of the sequence.
2214
2203
2215
2204
Raises
2216
2205
------
2217
2206
TypeError : PeriodDType data is passed
2218
2207
"""
2219
- inferred_freq = None
2220
-
2221
- data , copy = dtl .ensure_arraylike_for_datetimelike (
2222
- data , copy , cls_name = "DatetimeArray"
2223
- )
2224
-
2225
- if isinstance (data , DatetimeArray ):
2226
- inferred_freq = data .freq
2227
2208
2228
2209
# By this point we are assured to have either a numpy array or Index
2229
2210
data , copy = maybe_convert_dtype (data , copy , tz = tz )
@@ -2236,6 +2217,7 @@ def _sequence_to_dt64(
2236
2217
if data_dtype == object or is_string_dtype (data_dtype ):
2237
2218
# TODO: We do not have tests specific to string-dtypes,
2238
2219
# also complex or categorical or other extension
2220
+ data = cast (np .ndarray , data )
2239
2221
copy = False
2240
2222
if lib .infer_dtype (data , skipna = False ) == "integer" :
2241
2223
data = data .astype (np .int64 )
@@ -2248,7 +2230,7 @@ def _sequence_to_dt64(
2248
2230
yearfirst = yearfirst ,
2249
2231
creso = abbrev_to_npy_unit (out_unit ),
2250
2232
)
2251
- return result , tz , None
2233
+ return result , tz
2252
2234
else :
2253
2235
converted , inferred_tz = objects_to_datetime64 (
2254
2236
data ,
@@ -2273,14 +2255,15 @@ def _sequence_to_dt64(
2273
2255
result , _ = _construct_from_dt64_naive (
2274
2256
converted , tz = tz , copy = copy , ambiguous = ambiguous
2275
2257
)
2276
- return result , tz , None
2258
+ return result , tz
2277
2259
2278
2260
data_dtype = data .dtype
2279
2261
2280
2262
# `data` may have originally been a Categorical[datetime64[ns, tz]],
2281
2263
# so we need to handle these types.
2282
2264
if isinstance (data_dtype , DatetimeTZDtype ):
2283
2265
# DatetimeArray -> ndarray
2266
+ data = cast (DatetimeArray , data )
2284
2267
tz = _maybe_infer_tz (tz , data .tz )
2285
2268
result = data ._ndarray
2286
2269
@@ -2289,6 +2272,7 @@ def _sequence_to_dt64(
2289
2272
if isinstance (data , DatetimeArray ):
2290
2273
data = data ._ndarray
2291
2274
2275
+ data = cast (np .ndarray , data )
2292
2276
result , copy = _construct_from_dt64_naive (
2293
2277
data , tz = tz , copy = copy , ambiguous = ambiguous
2294
2278
)
@@ -2299,6 +2283,7 @@ def _sequence_to_dt64(
2299
2283
if data .dtype != INT64_DTYPE :
2300
2284
data = data .astype (np .int64 , copy = False )
2301
2285
copy = False
2286
+ data = cast (np .ndarray , data )
2302
2287
result = data .view (out_dtype )
2303
2288
2304
2289
if copy :
@@ -2308,7 +2293,7 @@ def _sequence_to_dt64(
2308
2293
assert result .dtype .kind == "M"
2309
2294
assert result .dtype != "M8"
2310
2295
assert is_supported_unit (get_unit_from_dtype (result .dtype ))
2311
- return result , tz , inferred_freq
2296
+ return result , tz
2312
2297
2313
2298
2314
2299
def _construct_from_dt64_naive (
0 commit comments