8000 REF/ENH: Constructors for DatetimeArray/TimedeltaArray by jbrockmendel · Pull Request #23493 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

REF/ENH: Constructors for DatetimeArray/TimedeltaArray #23493

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
implement maybe_validate_freq
  • Loading branch information
jbrockmendel committed Nov 5, 2018
commit 3a6263321d7740e6f8039a04a690e686a934fde7
20 changes: 20 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,26 @@ def maybe_define_freq(freq_infer, result):
result.freq = frequencies.to_offset(inferred)


def maybe_validate_freq(result, verify, freq, freq_infer, **kwargs):
"""
If a frequency was passed by the user and not inferred or extracted
from the underlying data, then validate that the data is consistent with
the user-provided frequency.

Parameters
----------
result : DatetimeIndex or TimedeltaIndex
verify : bool
freq : DateOffset or None
freq_infer : bool
**kwargs : arguments to pass to `_validate_frequency`
For DatetimeIndex this is just "ambiguous", empty for TimedeltaIndex
"""
if verify and len(result) > 0:
if freq is not None and not freq_infer:
result._validate_frequency(result, freq, **kwargs)


def validate_tz_from_dtype(dtype, tz):
"""
If the given dtype is a DatetimeTZDtype, extract the implied
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,8 @@ def __new__(cls, data=None,
assert subarr.dtype == 'M8[ns]', subarr.dtype

subarr = cls._simple_new(subarr, name=name, freq=freq, tz=tz)

if verify_integrity and len(subarr) > 0:
if freq is not None and not freq_infer:
cls._validate_frequency(subarr, freq, ambiguous=ambiguous)

dtl.maybe_validate_freq(subarr, verify_integrity, freq, freq_infer,
ambiguous=ambiguous)
dtl.maybe_define_freq(freq_infer, subarr)
return subarr._deepcopy_if_needed(ref_to_data, copy)

Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
assert data.dtype == 'm8[ns]', data.dtype

subarr = cls._simple_new(data, name=name, freq=freq)
# check that we are matching freqs
if verify_integrity and len(subarr) > 0:
if freq is not None and not freq_infer:
cls._validate_frequency(subarr, freq)

dtl.maybe_validate_freq(subarr, verify_integrity, freq, freq_infer)
dtl.maybe_define_freq(freq_infer, subarr)
return subarr

Expand Down
0