Skip to content

Extraneous parts broken off from other DTA/TDA PRs #23518

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

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 4 additions & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from pandas._libs.tslibs.period import (
Period, DIFFERENT_FREQ_INDEX, IncompatibleFrequency)

from pandas.errors import NullFrequencyError, PerformanceWarning
from pandas.errors import (
AbstractMethodError, NullFrequencyError, PerformanceWarning)
from pandas import compat

from pandas.tseries import frequencies
Expand Down Expand Up @@ -78,12 +79,10 @@ class AttributesMixin(object):
@property
def _attributes(self):
# Inheriting subclass should implement _attributes as a list of strings
from pandas.errors import AbstractMethodError
raise AbstractMethodError(self)

@classmethod
def _simple_new(cls, values, **kwargs):
from pandas.errors import AbstractMethodError
raise AbstractMethodError(cls)

def _get_attributes_dict(self):
Expand All @@ -108,7 +107,7 @@ def _box_func(self):
"""
box function to get object from internal representation
"""
raise com.AbstractMethodError(self)
raise AbstractMethodError(self)

def _box_values(self, values):
"""
Expand Down Expand Up @@ -337,7 +336,7 @@ def _sub_period(self, other):
.format(cls=type(self).__name__))

def _add_offset(self, offset):
raise com.AbstractMethodError(self)
raise AbstractMethodError(self)

def _add_delta(self, other):
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class DatetimeArrayMixin(dtl.DatetimeLikeArrayMixin):
# Constructors

_attributes = ["freq", "tz"]
_tz = None
_freq = None

@classmethod
def _simple_new(cls, values, freq=None, tz=None, **kwargs):
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray):

# --------------------------------------------------------------------
# Constructors
def __init__(self, values, freq=None, copy=False):

def __init__(self, values, freq=None, dtype=None, copy=False):
freq = dtl.validate_dtype_freq(dtype, freq)

if freq is not None:
freq = Period._maybe_convert_freq(freq)

Expand Down
31 changes: 22 additions & 9 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class DatetimeIndex(DatetimeArrayMixin, DatelikeOps, TimelikeOps,

"""
_resolution = cache_readonly(DatetimeArrayMixin._resolution.fget)
_shallow_copy = Index._shallow_copy

_typ = 'datetimeindex'
_join_precedence = 10
Expand All @@ -199,11 +198,15 @@ def _join_i8_wrapper(joinf, **kwargs):

_engine_type = libindex.DatetimeEngine

tz = None
_tz = None
_freq = None
_comparables = ['name', 'freqstr', 'tz']
_attributes = ['name', 'freq', 'tz']

# dummy attribute so that datetime.__eq__(DatetimeArray) defers
# by returning NotImplemented
timetuple = None
Copy link
Member

Choose a reason for hiding this comment

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

This is not yet tested I suppose then?

Copy link
Member Author

Choose a reason for hiding this comment

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

That is accurate.


# define my properties & methods for delegation
_bool_ops = ['is_month_start', 'is_month_end',
'is_quarter_start', 'is_quarter_end', 'is_year_start',
Expand All @@ -226,6 +229,9 @@ def _join_i8_wrapper(joinf, **kwargs):
_timezone = cache_readonly(DatetimeArrayMixin._timezone.fget)
is_normalized = cache_readonly(DatetimeArrayMixin.is_normalized.fget)

# --------------------------------------------------------------------
# Constructors

def __new__(cls, data=None,
freq=None, start=None, end=None, periods=None, tz=None,
normalize=False, closed=None, ambiguous='raise',
Expand Down Expand Up @@ -280,7 +286,7 @@ def __new__(cls, data=None,
data = data.tz_localize(tz, ambiguous=ambiguous)
else:
# the tz's must match
if str(tz) != str(data.tz):
if not timezones.tz_compare(tz, data.tz):
msg = ('data is already tz-aware {0}, unable to '
'set specified tz: {1}')
raise TypeError(msg.format(data.tz, tz))
Expand Down Expand Up @@ -327,12 +333,6 @@ def __new__(cls, data=None,

return subarr._deepcopy_if_needed(ref_to_data, copy)

def _convert_for_op(self, value):
""" Convert value to be insertable to ndarray """
if self._has_same_tz(value):
return _to_m8(value)
raise ValueError('Passed item and index have different timezone')

@classmethod
def _simple_new(cls, values, name=None, freq=None, tz=None,
dtype=None, **kwargs):
Expand All @@ -349,6 +349,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
result._reset_identity()
return result

# --------------------------------------------------------------------

@property
def _values(self):
# tz-naive -> ndarray
Expand Down Expand Up @@ -448,6 +450,12 @@ def __setstate__(self, state):
raise Exception("invalid pickle state")
_unpickle_compat = __setstate__

def _convert_for_op(self, value):
""" Convert value to be insertable to ndarray """
if self._has_same_tz(value):
return _to_m8(value)
raise ValueError('Passed item and index have different timezone')

def _maybe_update_attributes(self, attrs):
""" Update Index attributes (e.g. freq) depending on op """
freq = attrs.get('freq', None)
Expand Down Expand Up @@ -1104,6 +1112,9 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
else:
raise

# --------------------------------------------------------------------
# Wrapping DatetimeArray

year = wrap_field_accessor(DatetimeArrayMixin.year)
month = wrap_field_accessor(DatetimeArrayMixin.month)
day = wrap_field_accessor(DatetimeArrayMixin.day)
Expand Down Expand Up @@ -1142,6 +1153,8 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
month_name = wrap_array_method(DatetimeArrayMixin.month_name, True)
day_name = wrap_array_method(DatetimeArrayMixin.day_name, True)

# --------------------------------------------------------------------

@Substitution(klass='DatetimeIndex')
@Appender(_shared_docs['searchsorted'])
def searchsorted(self, value, side='left', sorter=None):
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
result._reset_identity()
return result

_shallow_copy = Index._shallow_copy

@property
def _formatter_func(self):
from pandas.io.formats.format import _get_format_timedelta64
Expand Down Expand Up @@ -243,13 +241,18 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
nat_rep=na_rep,
justify='all').get_result()

# -------------------------------------------------------------------
# Wrapping TimedeltaArray

days = wrap_field_accessor(TimedeltaArrayMixin.days)
seconds = wrap_field_accessor(TimedeltaArrayMixin.seconds)
microseconds = wrap_field_accessor(TimedeltaArrayMixin.microseconds)
nanoseconds = wrap_field_accessor(TimedeltaArrayMixin.nanoseconds)

total_seconds = wrap_array_method(TimedeltaArrayMixin.total_seconds, True)

# -------------------------------------------------------------------

@Appender(_index_shared_docs['astype'])
def astype(self, dtype, copy=True):
dtype = pandas_dtype(dtype)
Expand Down