Skip to content

Commit c9fb018

Browse files
authored
REF: sequence_to_dt64ns (pandas-dev#49040)
* REF: sequence_to_dt64ns * update test * update test
1 parent feea326 commit c9fb018

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

pandas/core/arrays/datetimes.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,33 @@ def _from_sequence_not_strict(
308308
):
309309
explicit_none = freq is None
310310
freq = freq if freq is not lib.no_default else None
311-
312311
freq, freq_infer = dtl.maybe_infer_freq(freq)
313312

313+
explicit_tz_none = tz is None
314+
if tz is lib.no_default:
315+
tz = None
316+
else:
317+
tz = timezones.maybe_get_tz(tz)
318+
319+
dtype = _validate_dt64_dtype(dtype)
320+
# if dtype has an embedded tz, capture it
321+
tz = validate_tz_from_dtype(dtype, tz, explicit_tz_none)
322+
314323
subarr, tz, inferred_freq = _sequence_to_dt64ns(
315324
data,
316-
dtype=dtype,
317325
copy=copy,
318326
tz=tz,
319327
dayfirst=dayfirst,
320328
yearfirst=yearfirst,
321329
ambiguous=ambiguous,
322330
)
331+
# We have to call this again after possibly inferring a tz above
332+
validate_tz_from_dtype(dtype, tz, explicit_tz_none)
333+
if tz is not None and explicit_tz_none:
334+
raise ValueError(
335+
"Passed data is timezone-aware, incompatible with 'tz=None'. "
336+
"Use obj.tz_localize(None) instead."
337+
)
323338

324339
freq, freq_infer = dtl.validate_inferred_freq(freq, inferred_freq, freq_infer)
325340
if explicit_none:
@@ -1995,13 +2010,12 @@ def sequence_to_datetimes(data, require_iso8601: bool = False) -> DatetimeArray:
19952010

19962011
def _sequence_to_dt64ns(
19972012
data,
1998-
dtype=None,
2013+
*,
19992014
copy: bool = False,
2000-
tz=lib.no_default,
2015+
tz: tzinfo | None = None,
20012016
dayfirst: bool = False,
20022017
yearfirst: bool = False,
20032018
ambiguous: TimeAmbiguous = "raise",
2004-
*,
20052019
allow_mixed: bool = False,
20062020
require_iso8601: bool = False,
20072021
):
@@ -2034,18 +2048,8 @@ def _sequence_to_dt64ns(
20342048
------
20352049
TypeError : PeriodDType data is passed
20362050
"""
2037-
explicit_tz_none = tz is None
2038-
if tz is lib.no_default:
2039-
tz = None
2040-
20412051
inferred_freq = None
20422052

2043-
dtype = _validate_dt64_dtype(dtype)
2044-
tz = timezones.maybe_get_tz(tz)
2045-
2046-
# if dtype has an embedded tz, capture it
2047-
tz = validate_tz_from_dtype(dtype, tz, explicit_tz_none)
2048-
20492053
data, copy = dtl.ensure_arraylike_for_datetimelike(
20502054
data, copy, cls_name="DatetimeArray"
20512055
)
@@ -2138,15 +2142,6 @@ def _sequence_to_dt64ns(
21382142

21392143
assert isinstance(result, np.ndarray), type(result)
21402144
assert result.dtype == "M8[ns]", result.dtype
2141-
2142-
# We have to call this again after possibly inferring a tz above
2143-
validate_tz_from_dtype(dtype, tz, explicit_tz_none)
2144-
if tz is not None and explicit_tz_none:
2145-
raise ValueError(
2146-
"Passed data is timezone-aware, incompatible with 'tz=None'. "
2147-
"Use obj.tz_localize(None) instead."
2148-
)
2149-
21502145
return result, tz, inferred_freq
21512146

21522147

pandas/tests/arrays/datetimes/test_constructors.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,15 @@ def test_tz_dtype_mismatch_raises(self):
134134
["2000"], dtype=DatetimeTZDtype(tz="US/Central")
135135
)
136136
with pytest.raises(TypeError, match="data is already tz-aware"):
137-
_sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="UTC"))
137+
DatetimeArray._from_sequence_not_strict(
138+
arr, dtype=DatetimeTZDtype(tz="UTC")
139+
)
138140

139141
def test_tz_dtype_matches(self):
140-
arr = DatetimeArray._from_sequence(
141-
["2000"], dtype=DatetimeTZDtype(tz="US/Central")
142-
)
143-
result, _, _ = _sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="US/Central"))
144-
tm.assert_numpy_array_equal(arr._data, result)
142+
dtype = DatetimeTZDtype(tz="US/Central")
143+
arr = DatetimeArray._from_sequence(["2000"], dtype=dtype)
144+
result = DatetimeArray._from_sequence_not_strict(arr, dtype=dtype)
145+
tm.assert_equal(arr, result)
145146

146147
@pytest.mark.parametrize("order", ["F", "C"])
147148
def test_2d(self, order):

0 commit comments

Comments
 (0)