From c230f44527d797b5f8eb9a22e4032a46b17d8ecd Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 18 Nov 2021 13:12:16 -0800 Subject: [PATCH] REF: remove unused allow_object kwarg from sequence_to_dt64ns --- pandas/core/arrays/datetimes.py | 37 +++---------------- pandas/core/dtypes/cast.py | 6 +-- .../arrays/datetimes/test_constructors.py | 14 +++---- pandas/tests/arrays/test_datetimelike.py | 6 +-- 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index a0a7ef3501d7f..460bfda56276d 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -9,7 +9,6 @@ from typing import ( TYPE_CHECKING, Literal, - overload, ) import warnings @@ -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, @@ -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, @@ -2015,7 +1995,6 @@ def sequence_to_dt64ns( yearfirst=False, ambiguous="raise", *, - allow_object: bool = False, allow_mixed: bool = False, require_iso8601: bool = False, ): @@ -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 @@ -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, ) @@ -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 diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 6d5162f3fe3a4..7c0566079a7d0 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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. is not convertible to datetime return v.reshape(shape) @@ -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 @@ -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: diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index cd7d9a479ab38..e6c65499f6fcc 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -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: @@ -42,7 +42,7 @@ def test_freq_validation(self): "meth", [ DatetimeArray._from_sequence, - sequence_to_dt64ns, + _sequence_to_dt64ns, pd.to_datetime, pd.DatetimeIndex, ], @@ -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) @@ -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"]) @@ -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] diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 5aa20bedc4a48..ff7c7a7782da3 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -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 @@ -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) @@ -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)