@@ -1904,6 +1904,23 @@ def std(
1904
1904
# Constructor Helpers
1905
1905
1906
1906
1907
+ def sequence_to_datetimes (
1908
+ data , allow_object : bool = False
1909
+ ) -> Union [np .ndarray , DatetimeArray ]:
1910
+ """
1911
+ Parse/convert the passed data to either DatetimeArray or np.ndarray[object].
1912
+ """
1913
+ result , tz , freq = sequence_to_dt64ns (
1914
+ data , allow_object = allow_object , allow_mixed = True
1915
+ )
1916
+ if result .dtype == object :
1917
+ return result
1918
+
1919
+ dtype = tz_to_dtype (tz )
1920
+ dta = DatetimeArray ._simple_new (result , freq = freq , dtype = dtype )
1921
+ return dta
1922
+
1923
+
1907
1924
def sequence_to_dt64ns (
1908
1925
data ,
1909
1926
dtype = None ,
@@ -1912,6 +1929,9 @@ def sequence_to_dt64ns(
1912
1929
dayfirst = False ,
1913
1930
yearfirst = False ,
1914
1931
ambiguous = "raise" ,
1932
+ * ,
1933
+ allow_object : bool = False ,
1934
+ allow_mixed : bool = False ,
1915
1935
):
1916
1936
"""
1917
1937
Parameters
@@ -1924,6 +1944,11 @@ def sequence_to_dt64ns(
1924
1944
yearfirst : bool, default False
1925
1945
ambiguous : str, bool, or arraylike, default 'raise'
1926
1946
See pandas._libs.tslibs.tzconversion.tz_localize_to_utc.
1947
+ allow_object : bool, default False
1948
+ Whether to return an object-dtype ndarray instead of raising if the
1949
+ data contains more than one timezone.
1950
+ allow_mixed : bool, default False
1951
+ Interpret integers as timestamps when datetime objects are also present.
1927
1952
1928
1953
Returns
1929
1954
-------
@@ -1987,14 +2012,21 @@ def sequence_to_dt64ns(
1987
2012
# data comes back here as either i8 to denote UTC timestamps
1988
2013
# or M8[ns] to denote wall times
1989
2014
data , inferred_tz = objects_to_datetime64ns (
1990
- data , dayfirst = dayfirst , yearfirst = yearfirst
2015
+ data ,
2016
+ dayfirst = dayfirst ,
2017
+ yearfirst = yearfirst ,
2018
+ allow_object = allow_object ,
2019
+ allow_mixed = allow_mixed ,
1991
2020
)
1992
2021
if tz and inferred_tz :
1993
2022
# two timezones: convert to intended from base UTC repr
1994
2023
data = tzconversion .tz_convert_from_utc (data .view ("i8" ), tz )
1995
2024
data = data .view (DT64NS_DTYPE )
1996
2025
elif inferred_tz :
1997
2026
tz = inferred_tz
2027
+ elif allow_object and data .dtype == object :
2028
+ # We encountered mixed-timezones.
2029
+ return data , None , None
1998
2030
1999
2031
data_dtype = data .dtype
2000
2032
@@ -2053,6 +2085,7 @@ def objects_to_datetime64ns(
2053
2085
errors = "raise" ,
2054
2086
require_iso8601 = False ,
2055
2087
allow_object = False ,
2088
+ allow_mixed : bool = False ,
2056
2089
):
2057
2090
"""
2058
2091
Convert data to array of timestamps.
@@ -2069,6 +2102,8 @@ def objects_to_datetime64ns(
2069
2102
allow_object : bool
2070
2103
Whether to return an object-dtype ndarray instead of raising if the
2071
2104
data contains more than one timezone.
2105
+ allow_mixed : bool, default False
2106
+ Interpret integers as timestamps when datetime objects are also present.
2072
2107
2073
2108
Returns
2074
2109
-------
@@ -2097,6 +2132,7 @@ def objects_to_datetime64ns(
2097
2132
dayfirst = dayfirst ,
2098
2133
yearfirst = yearfirst ,
2099
2134
require_iso8601 = require_iso8601 ,
2135
+ allow_mixed = allow_mixed ,
2100
2136
)
2101
2137
result = result .reshape (data .shape , order = order )
2102
2138
except ValueError as err :
@@ -2133,7 +2169,7 @@ def objects_to_datetime64ns(
2133
2169
raise TypeError (result )
2134
2170
2135
2171
2136
- def maybe_convert_dtype (data , copy ):
2172
+ def maybe_convert_dtype (data , copy : bool ):
2137
2173
"""
2138
2174
Convert data based on dtype conventions, issuing deprecation warnings
2139
2175
or errors where appropriate.
0 commit comments