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
Next Next commit
implement maybe_define_freq
  • Loading branch information
jbrockmendel committed Nov 4, 2018
commit 98dca45aaf030a3a42ab40dbe4714f02dd553e7c
16 changes: 16 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,22 @@ def maybe_infer_freq(freq):
return freq, freq_infer


def maybe_define_freq(freq_infer, result):
"""
If appropriate, infer the frequency of the given Datetime/Timedelta Array
and pin it to the object at the end of the construction.

Parameters
----------
freq_infer : bool
result : DatetimeArray or TimedeltaArray
"""
if freq_infer:
inferred = result.inferred_freq
if inferred:
result.freq = frequencies.to_offset(inferred)


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/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __new__(cls, values, freq=None, tz=None, dtype=None, copy=False):
if lib.is_scalar(values):
raise ValueError('{cls}() must be called with a '
'collection of some kind, {data} was passed'
.format(cls=cls.__name__, data=repr(data)))
.format(cls=cls.__name__, data=repr(values)))
elif isinstance(values, DatetimeArrayMixin):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DatetimeArrayMixin -> cls?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you don't need to get the tz in this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DatetimeArrayMixin -> cls?

No. For the moment we are still using inheritance, so this would mess up for DatetimeIndex == DatetimeArray. When we change to composition this check will have to become isinstance(values, (DatetimeArray, ABCDatetimeIndex))

# extract nanosecond unix timestamps
values = values.asi8
Expand All @@ -230,10 +230,7 @@ def __new__(cls, values, freq=None, tz=None, dtype=None, copy=False):
values = conversion.ensure_datetime64ns(values, copy=copy)

result = cls._simple_new(values, freq=freq, tz=tz)
if freq_infer:
inferred = result.inferred_freq
if inferred:
result.freq = to_offset(inferred)
dtl.maybe_define_freq(freq_infer, result)

# NB: Among other things not yet ported from the DatetimeIndex
# constructor, this does not call _deepcopy_if_needed
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pandas.core.algorithms import checked_add_with_arr

from pandas.tseries.offsets import Tick
from pandas.tseries.frequencies import to_offset

from . import datetimelike as dtl

Expand Down Expand Up @@ -152,11 +151,7 @@ def __new__(cls, values, freq=None, dtype=_TD_DTYPE, copy=False):
values = values.astype(_TD_DTYPE)

result = cls._simple_new(values, freq=freq)
if freq_infer:
inferred = result.inferred_freq
if inferred:
result.freq = to_offset(inferred)

dtl.maybe_define_freq(freq_infer, result)
return result

@classmethod
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def __new__(cls, data=None,
.format(cls=cls.__name__, data=repr(data)))

elif not isinstance(data, (np.ndarray, Index, ABCSeries,
DatetimeArrayMixin)):
DatetimeArrayMixin)):
if not isinstance(data, (list, tuple)):
data = list(data)
data = np.asarray(data, dtype='O')
Expand Down Expand Up @@ -321,11 +321,7 @@ def __new__(cls, data=None,
if freq is not None and not freq_infer:
cls._validate_frequency(subarr, freq, ambiguous=ambiguous)

if freq_infer:
inferred = subarr.inferred_freq
if inferred:
subarr.freq = to_offset(inferred)

dtl.maybe_define_freq(freq_infer, subarr)
return subarr._deepcopy_if_needed(ref_to_data, copy)

@classmethod
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
to_timedelta, _coerce_scalar_to_timedelta_type)
from pandas._libs import (lib, index as libindex,
join as libjoin, Timedelta, NaT)
from pandas._libs.tslibs.timedeltas import array_to_timedelta64


class TimedeltaIndex(TimedeltaArrayMixin, DatetimeIndexOpsMixin,
Expand Down Expand Up @@ -182,11 +181,7 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
if freq is not None and not freq_infer:
cls._validate_frequency(subarr, freq)

if freq_infer:
inferred = subarr.inferred_freq
if inferred:
subarr.freq = to_offset(inferred)

dtl.maybe_define_freq(freq_infer, subarr)
return subarr

@classmethod
Expand Down
0