Skip to content

Commit

Permalink
REF: sequence_to_dt64ns (pandas-dev#49040)
Browse files Browse the repository at this point in the history
* REF: sequence_to_dt64ns

* update test

* update test
  • Loading branch information
jbrockmendel committed Oct 11, 2022
1 parent feea326 commit c9fb018
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
43 changes: 19 additions & 24 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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


Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/arrays/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ 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(
["2000"], dtype=DatetimeTZDtype(tz="US/Central")
)
result, _, _ = _sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="US/Central"))
tm.assert_numpy_array_equal(arr._data, result)
dtype = DatetimeTZDtype(tz="US/Central")
arr = DatetimeArray._from_sequence(["2000"], dtype=dtype)
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):
Expand Down

0 comments on commit c9fb018

Please sign in to comment.