From a4131fea30db546df188e23137e1e429be47df82 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 10 Oct 2022 17:57:16 -0700 Subject: [PATCH 1/3] REF: sequence_to_dt64ns --- pandas/core/arrays/datetimes.py | 43 ++++++++----------- .../arrays/datetimes/test_constructors.py | 4 +- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index a9c1a7e3cdab0..f797222753183 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -308,18 +308,33 @@ def _from_sequence_not_strict( ): explicit_none = freq is None freq = freq if freq is not lib.no_default else None - freq, freq_infer = dtl.maybe_infer_freq(freq) + explicit_tz_none = tz is None + if tz is lib.no_default: + tz = None + else: + tz = timezones.maybe_get_tz(tz) + + dtype = _validate_dt64_dtype(dtype) + # if dtype has an embedded tz, capture it + tz = validate_tz_from_dtype(dtype, tz, explicit_tz_none) + subarr, tz, inferred_freq = _sequence_to_dt64ns( data, - dtype=dtype, copy=copy, tz=tz, dayfirst=dayfirst, yearfirst=yearfirst, ambiguous=ambiguous, ) + # We have to call this again after possibly inferring a tz above + validate_tz_from_dtype(dtype, tz, explicit_tz_none) + if tz is not None and explicit_tz_none: + raise ValueError( + "Passed data is timezone-aware, incompatible with 'tz=None'. " + "Use obj.tz_localize(None) instead." + ) freq, freq_infer = dtl.validate_inferred_freq(freq, inferred_freq, freq_infer) if explicit_none: @@ -1995,13 +2010,12 @@ def sequence_to_datetimes(data, require_iso8601: bool = False) -> DatetimeArray: def _sequence_to_dt64ns( data, - dtype=None, + *, copy: bool = False, - tz=lib.no_default, + tz: tzinfo | None = None, dayfirst: bool = False, yearfirst: bool = False, ambiguous: TimeAmbiguous = "raise", - *, allow_mixed: bool = False, require_iso8601: bool = False, ): @@ -2034,18 +2048,8 @@ def _sequence_to_dt64ns( ------ TypeError : PeriodDType data is passed """ - explicit_tz_none = tz is None - if tz is lib.no_default: - tz = None - inferred_freq = None - dtype = _validate_dt64_dtype(dtype) - tz = timezones.maybe_get_tz(tz) - - # if dtype has an embedded tz, capture it - tz = validate_tz_from_dtype(dtype, tz, explicit_tz_none) - data, copy = dtl.ensure_arraylike_for_datetimelike( data, copy, cls_name="DatetimeArray" ) @@ -2138,15 +2142,6 @@ def _sequence_to_dt64ns( assert isinstance(result, np.ndarray), type(result) assert result.dtype == "M8[ns]", result.dtype - - # We have to call this again after possibly inferring a tz above - validate_tz_from_dtype(dtype, tz, explicit_tz_none) - if tz is not None and explicit_tz_none: - raise ValueError( - "Passed data is timezone-aware, incompatible with 'tz=None'. " - "Use obj.tz_localize(None) instead." - ) - return result, tz, inferred_freq diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index cb2d8f31f0f9c..b8f94f67c7d73 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -134,7 +134,9 @@ def test_tz_dtype_mismatch_raises(self): ["2000"], dtype=DatetimeTZDtype(tz="US/Central") ) with pytest.raises(TypeError, match="data is already tz-aware"): - _sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="UTC")) + DatetimeArray._from_sequence_not_strict( + arr, dtype=DatetimeTZDtype(tz="UTC") + ) def test_tz_dtype_matches(self): arr = DatetimeArray._from_sequence( From 19d8de6612d0d335e310c9a672cfb54364327db4 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 10 Oct 2022 19:21:11 -0700 Subject: [PATCH 2/3] update test --- pandas/tests/arrays/datetimes/test_constructors.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index b8f94f67c7d73..30e17dab43205 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -139,11 +139,12 @@ def test_tz_dtype_mismatch_raises(self): ) def test_tz_dtype_matches(self): + dtype = DatetimeTZDtype(tz="US/Central") arr = DatetimeArray._from_sequence( - ["2000"], dtype=DatetimeTZDtype(tz="US/Central") + ["2000"], dtype=dtype ) - result, _, _ = _sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="US/Central")) - tm.assert_numpy_array_equal(arr._data, result) + result = DatetimeArray._from_sequence_not_strict(arr, dtype=dtype) + tm.assert_equal(arr, result) @pytest.mark.parametrize("order", ["F", "C"]) def test_2d(self, order): From 4ff420833bfd228f6d21f937a279f64e53875ddb Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 10 Oct 2022 19:22:38 -0700 Subject: [PATCH 3/3] update test --- pandas/tests/arrays/datetimes/test_constructors.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index 30e17dab43205..992d047b1afef 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -140,9 +140,7 @@ def test_tz_dtype_mismatch_raises(self): def test_tz_dtype_matches(self): dtype = DatetimeTZDtype(tz="US/Central") - arr = DatetimeArray._from_sequence( - ["2000"], dtype=dtype - ) + arr = DatetimeArray._from_sequence(["2000"], dtype=dtype) result = DatetimeArray._from_sequence_not_strict(arr, dtype=dtype) tm.assert_equal(arr, result)