Skip to content

Commit 4ae63aa

Browse files
jbrockmendeljreback
authored andcommitted
Implement DatetimeArray._from_sequence (#24074)
1 parent 2643721 commit 4ae63aa

File tree

6 files changed

+329
-274
lines changed

6 files changed

+329
-274
lines changed

pandas/core/arrays/datetimelike.py

+36-75
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77

88
from pandas._libs import NaT, iNaT, lib
9-
from pandas._libs.tslibs import timezones
109
from pandas._libs.tslibs.period import (
1110
DIFFERENT_FREQ_INDEX, IncompatibleFrequency, Period)
1211
from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds
@@ -21,8 +20,7 @@
2120
is_bool_dtype, is_datetime64_any_dtype, is_datetime64_dtype,
2221
is_datetime64tz_dtype, is_extension_array_dtype, is_float_dtype,
2322
is_integer_dtype, is_list_like, is_object_dtype, is_offsetlike,
24-
is_period_dtype, is_timedelta64_dtype, needs_i8_conversion, pandas_dtype)
25-
from pandas.core.dtypes.dtypes import DatetimeTZDtype
23+
is_period_dtype, is_timedelta64_dtype, needs_i8_conversion)
2624
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
2725
from pandas.core.dtypes.missing import isna
2826

@@ -1113,6 +1111,41 @@ def validate_endpoints(closed):
11131111
return left_closed, right_closed
11141112

11151113

1114+
def validate_inferred_freq(freq, inferred_freq, freq_infer):
1115+
"""
1116+
If the user passes a freq and another freq is inferred from passed data,
1117+
require that they match.
1118+
1119+
Parameters
1120+
----------
1121+
freq : DateOffset or None
1122+
inferred_freq : DateOffset or None
1123+
freq_infer : bool
1124+
1125+
Returns
1126+
-------
1127+
freq : DateOffset or None
1128+
freq_infer : bool
1129+
1130+
Notes
1131+
-----
1132+
We assume at this point that `maybe_infer_freq` has been called, so
1133+
`freq` is either a DateOffset object or None.
1134+
"""
1135+
if inferred_freq is not None:
1136+
if freq is not None and freq != inferred_freq:
1137+
raise ValueError('Inferred frequency {inferred} from passed '
1138+
'values does not conform to passed frequency '
1139+
'{passed}'
1140+
.format(inferred=inferred_freq,
1141+
passed=freq.freqstr))
1142+
elif freq is None:
1143+
freq = inferred_freq
1144+
freq_infer = False
1145+
1146+
return freq, freq_infer
1147+
1148+
11161149
def maybe_infer_freq(freq):
11171150
"""
11181151
Comparing a DateOffset to the string "infer" raises, so we need to
@@ -1140,78 +1173,6 @@ def maybe_infer_freq(freq):
11401173
return freq, freq_infer
11411174

11421175

1143-
def validate_tz_from_dtype(dtype, tz):
1144-
"""
1145-
If the given dtype is a DatetimeTZDtype, extract the implied
1146-
tzinfo object from it and check that it does not conflict with the given
1147-
tz.
1148-
1149-
Parameters
1150-
----------
1151-
dtype : dtype, str
1152-
tz : None, tzinfo
1153-
1154-
Returns
1155-
-------
1156-
tz : consensus tzinfo
1157-
1158-
Raises
1159-
------
1160-
ValueError : on tzinfo mismatch
1161-
"""
1162-
if dtype is not None:
1163-
if isinstance(dtype, compat.string_types):
1164-
try:
1165-
dtype = DatetimeTZDtype.construct_from_string(dtype)
1166-
except TypeError:
1167-
# Things like `datetime64[ns]`, which is OK for the
1168-
# constructors, but also nonsense, which should be validated
1169-
# but not by us. We *do* allow non-existent tz errors to
1170-
# go through
1171-
pass
1172-
dtz = getattr(dtype, 'tz', None)
1173-
if dtz is not None:
1174-
if tz is not None and not timezones.tz_compare(tz, dtz):
1175-
raise ValueError("cannot supply both a tz and a dtype"
1176-
" with a tz")
1177-
tz = dtz
1178-
return tz
1179-
1180-
1181-
def validate_dtype_freq(dtype, freq):
1182-
"""
1183-
If both a dtype and a freq are available, ensure they match. If only
1184-
dtype is available, extract the implied freq.
1185-
1186-
Parameters
1187-
----------
1188-
dtype : dtype
1189-
freq : DateOffset or None
1190-
1191-
Returns
1192-
-------
1193-
freq : DateOffset
1194-
1195-
Raises
1196-
------
1197-
ValueError : non-period dtype
1198-
IncompatibleFrequency : mismatch between dtype and freq
1199-
"""
1200-
if freq is not None:
1201-
freq = frequencies.to_offset(freq)
1202-
1203-
if dtype is not None:
1204-
dtype = pandas_dtype(dtype)
1205-
if not is_period_dtype(dtype):
1206-
raise ValueError('dtype must be PeriodDtype')
1207-
if freq is None:
1208-
freq = dtype.freq
1209-
elif freq != dtype.freq:
1210-
raise IncompatibleFrequency('specified freq and dtype '
1211-
'are different')
1212-
return freq
1213-
1214-
12151176
def _ensure_datetimelike_to_i8(other, to_utc=False):
12161177
"""
12171178
Helper for coercing an input scalar or array to i8.

0 commit comments

Comments
 (0)