Skip to content

REF: sequence_to_dt64ns #49040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to add a docstring for _from_sequence_not_strict while here. Not completely obvious what "not strict" means relative to _from_sequence

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. will add that in the follow-up branch

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