Skip to content

REF: remove unused allow_object kwarg from sequence_to_dt64ns #44519

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 2 commits into from
Nov 20, 2021
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
37 changes: 5 additions & 32 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import (
TYPE_CHECKING,
Literal,
overload,
)
import warnings

Expand Down Expand Up @@ -356,7 +355,7 @@ def _from_sequence_not_strict(

freq, freq_infer = dtl.maybe_infer_freq(freq)

subarr, tz, inferred_freq = sequence_to_dt64ns(
subarr, tz, inferred_freq = _sequence_to_dt64ns(
data,
dtype=dtype,
copy=copy,
Expand Down Expand Up @@ -1972,41 +1971,22 @@ def std(
# Constructor Helpers


@overload
def sequence_to_datetimes(
data, allow_object: Literal[False] = ..., require_iso8601: bool = ...
) -> DatetimeArray:
...


@overload
def sequence_to_datetimes(
data, allow_object: Literal[True] = ..., require_iso8601: bool = ...
) -> np.ndarray | DatetimeArray:
...


def sequence_to_datetimes(
data, allow_object: bool = False, require_iso8601: bool = False
) -> np.ndarray | DatetimeArray:
def sequence_to_datetimes(data, require_iso8601: bool = False) -> DatetimeArray:
"""
Parse/convert the passed data to either DatetimeArray or np.ndarray[object].
"""
result, tz, freq = sequence_to_dt64ns(
result, tz, freq = _sequence_to_dt64ns(
data,
allow_object=allow_object,
allow_mixed=True,
require_iso8601=require_iso8601,
)
if result.dtype == object:
return result

dtype = tz_to_dtype(tz)
dta = DatetimeArray._simple_new(result, freq=freq, dtype=dtype)
return dta


def sequence_to_dt64ns(
def _sequence_to_dt64ns(
data,
dtype=None,
copy=False,
Expand All @@ -2015,7 +1995,6 @@ def sequence_to_dt64ns(
yearfirst=False,
ambiguous="raise",
*,
allow_object: bool = False,
allow_mixed: bool = False,
require_iso8601: bool = False,
):
Expand All @@ -2030,9 +2009,6 @@ def sequence_to_dt64ns(
yearfirst : bool, default False
ambiguous : str, bool, or arraylike, default 'raise'
See pandas._libs.tslibs.tzconversion.tz_localize_to_utc.
allow_object : bool, default False
Whether to return an object-dtype ndarray instead of raising if the
data contains more than one timezone.
allow_mixed : bool, default False
Interpret integers as timestamps when datetime objects are also present.
require_iso8601 : bool, default False
Expand Down Expand Up @@ -2102,7 +2078,7 @@ def sequence_to_dt64ns(
data,
dayfirst=dayfirst,
yearfirst=yearfirst,
allow_object=allow_object,
allow_object=False,
allow_mixed=allow_mixed,
require_iso8601=require_iso8601,
)
Expand All @@ -2112,9 +2088,6 @@ def sequence_to_dt64ns(
data = data.view(DT64NS_DTYPE)
elif inferred_tz:
tz = inferred_tz
elif allow_object and data.dtype == object:
# We encountered mixed-timezones.
return data, None, None

data_dtype = data.dtype

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ def try_datetime(v: np.ndarray) -> ArrayLike:
try:
# GH#19671 we pass require_iso8601 to be relatively strict
# when parsing strings.
dta = sequence_to_datetimes(v, require_iso8601=True, allow_object=False)
dta = sequence_to_datetimes(v, require_iso8601=True)
except (ValueError, TypeError):
# e.g. <class 'numpy.timedelta64'> is not convertible to datetime
return v.reshape(shape)
Expand Down Expand Up @@ -1635,7 +1635,7 @@ def maybe_cast_to_datetime(

try:
if is_datetime64:
dta = sequence_to_datetimes(value, allow_object=False)
dta = sequence_to_datetimes(value)
# GH 25843: Remove tz information since the dtype
# didn't specify one

Expand Down Expand Up @@ -1663,7 +1663,7 @@ def maybe_cast_to_datetime(
# datetime64tz is assumed to be naive which should
# be localized to the timezone.
is_dt_string = is_string_dtype(value.dtype)
dta = sequence_to_datetimes(value, allow_object=False)
dta = sequence_to_datetimes(value)
if dta.tz is not None:
value = dta.astype(dtype, copy=False)
elif is_dt_string:
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/arrays/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import DatetimeArray
from pandas.core.arrays.datetimes import sequence_to_dt64ns
from pandas.core.arrays.datetimes import _sequence_to_dt64ns


class TestDatetimeArrayConstructor:
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_freq_validation(self):
"meth",
[
DatetimeArray._from_sequence,
sequence_to_dt64ns,
_sequence_to_dt64ns,
pd.to_datetime,
pd.DatetimeIndex,
],
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_bool_dtype_raises(self):
DatetimeArray._from_sequence(arr)

with pytest.raises(TypeError, match=msg):
sequence_to_dt64ns(arr)
_sequence_to_dt64ns(arr)

with pytest.raises(TypeError, match=msg):
pd.DatetimeIndex(arr)
Expand Down Expand Up @@ -128,13 +128,13 @@ 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"))
_sequence_to_dt64ns(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"))
result, _, _ = _sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="US/Central"))
tm.assert_numpy_array_equal(arr._data, result)

@pytest.mark.parametrize("order", ["F", "C"])
Expand All @@ -144,8 +144,8 @@ def test_2d(self, order):
if order == "F":
arr = arr.T

res = sequence_to_dt64ns(arr)
expected = sequence_to_dt64ns(arr.ravel())
res = _sequence_to_dt64ns(arr)
expected = _sequence_to_dt64ns(arr.ravel())

tm.assert_numpy_array_equal(res[0].ravel(), expected[0])
assert res[1] == expected[1]
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
PeriodArray,
TimedeltaArray,
)
from pandas.core.arrays.datetimes import sequence_to_dt64ns
from pandas.core.arrays.datetimes import _sequence_to_dt64ns
from pandas.core.arrays.timedeltas import sequence_to_td64ns


Expand Down Expand Up @@ -1361,7 +1361,7 @@ def test_from_pandas_array(dtype):
expected = cls._from_sequence(data)
tm.assert_extension_array_equal(result, expected)

func = {"M8[ns]": sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype]
func = {"M8[ns]": _sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype]
result = func(arr)[0]
expected = func(data)[0]
tm.assert_equal(result, expected)
Expand Down Expand Up @@ -1424,7 +1424,7 @@ def test_from_obscure_array(dtype, array_likes):
result = cls._from_sequence(data)
tm.assert_extension_array_equal(result, expected)

func = {"M8[ns]": sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype]
func = {"M8[ns]": _sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype]
result = func(arr)[0]
expected = func(data)[0]
tm.assert_equal(result, expected)
Expand Down