Skip to content

Commit c552b6a

Browse files
jbrockmendelPingviinituutti
authored andcommitted
Extraneous parts broken off from other PRS (pandas-dev#23518)
1 parent 3a26444 commit c552b6a

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

pandas/core/arrays/datetimelike.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from pandas._libs.tslibs.period import (
1313
Period, DIFFERENT_FREQ_INDEX, IncompatibleFrequency)
1414

15-
from pandas.errors import NullFrequencyError, PerformanceWarning
15+
from pandas.errors import (
16+
AbstractMethodError, NullFrequencyError, PerformanceWarning)
1617
from pandas import compat
1718

1819
from pandas.tseries import frequencies
@@ -78,12 +79,10 @@ class AttributesMixin(object):
7879
@property
7980
def _attributes(self):
8081
# Inheriting subclass should implement _attributes as a list of strings
81-
from pandas.errors import AbstractMethodError
8282
raise AbstractMethodError(self)
8383

8484
@classmethod
8585
def _simple_new(cls, values, **kwargs):
86-
from pandas.errors import AbstractMethodError
8786
raise AbstractMethodError(cls)
8887

8988
def _get_attributes_dict(self):
@@ -108,7 +107,7 @@ def _box_func(self):
108107
"""
109108
box function to get object from internal representation
110109
"""
111-
raise com.AbstractMethodError(self)
110+
raise AbstractMethodError(self)
112111

113112
def _box_values(self, values):
114113
"""
@@ -337,7 +336,7 @@ def _sub_period(self, other):
337336
.format(cls=type(self).__name__))
338337

339338
def _add_offset(self, offset):
340-
raise com.AbstractMethodError(self)
339+
raise AbstractMethodError(self)
341340

342341
def _add_delta(self, other):
343342
"""

pandas/core/arrays/datetimes.py

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class DatetimeArrayMixin(dtl.DatetimeLikeArrayMixin):
170170
# Constructors
171171

172172
_attributes = ["freq", "tz"]
173+
_tz = None
174+
_freq = None
173175

174176
@classmethod
175177
def _simple_new(cls, values, freq=None, tz=None, **kwargs):

pandas/core/arrays/period.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray):
165165

166166
# --------------------------------------------------------------------
167167
# Constructors
168-
def __init__(self, values, freq=None, copy=False):
168+
169+
def __init__(self, values, freq=None, dtype=None, copy=False):
170+
freq = dtl.validate_dtype_freq(dtype, freq)
171+
169172
if freq is not None:
170173
freq = Period._maybe_convert_freq(freq)
171174

pandas/core/indexes/datetimes.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ class DatetimeIndex(DatetimeArrayMixin, DatelikeOps, TimelikeOps,
182182
183183
"""
184184
_resolution = cache_readonly(DatetimeArrayMixin._resolution.fget)
185-
_shallow_copy = Index._shallow_copy
186185

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

200199
_engine_type = libindex.DatetimeEngine
201200

202-
tz = None
201+
_tz = None
203202
_freq = None
204203
_comparables = ['name', 'freqstr', 'tz']
205204
_attributes = ['name', 'freq', 'tz']
206205

206+
# dummy attribute so that datetime.__eq__(DatetimeArray) defers
207+
# by returning NotImplemented
208+
timetuple = None
209+
207210
# define my properties & methods for delegation
208211
_bool_ops = ['is_month_start', 'is_month_end',
209212
'is_quarter_start', 'is_quarter_end', 'is_year_start',
@@ -226,6 +229,9 @@ def _join_i8_wrapper(joinf, **kwargs):
226229
_timezone = cache_readonly(DatetimeArrayMixin._timezone.fget)
227230
is_normalized = cache_readonly(DatetimeArrayMixin.is_normalized.fget)
228231

232+
# --------------------------------------------------------------------
233+
# Constructors
234+
229235
def __new__(cls, data=None,
230236
freq=None, start=None, end=None, periods=None, tz=None,
231237
normalize=False, closed=None, ambiguous='raise',
@@ -280,7 +286,7 @@ def __new__(cls, data=None,
280286
data = data.tz_localize(tz, ambiguous=ambiguous)
281287
else:
282288
# the tz's must match
283-
if str(tz) != str(data.tz):
289+
if not timezones.tz_compare(tz, data.tz):
284290
msg = ('data is already tz-aware {0}, unable to '
285291
'set specified tz: {1}')
286292
raise TypeError(msg.format(data.tz, tz))
@@ -327,12 +333,6 @@ def __new__(cls, data=None,
327333

328334
return subarr._deepcopy_if_needed(ref_to_data, copy)
329335

330-
def _convert_for_op(self, value):
331-
""" Convert value to be insertable to ndarray """
332-
if self._has_same_tz(value):
333-
return _to_m8(value)
334-
raise ValueError('Passed item and index have different timezone')
335-
336336
@classmethod
337337
def _simple_new(cls, values, name=None, freq=None, tz=None,
338338
dtype=None, **kwargs):
@@ -349,6 +349,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
349349
result._reset_identity()
350350
return result
351351

352+
# --------------------------------------------------------------------
353+
352354
@property
353355
def _values(self):
354356
# tz-naive -> ndarray
@@ -448,6 +450,12 @@ def __setstate__(self, state):
448450
raise Exception("invalid pickle state")
449451
_unpickle_compat = __setstate__
450452

453+
def _convert_for_op(self, value):
454+
""" Convert value to be insertable to ndarray """
455+
if self._has_same_tz(value):
456+
return _to_m8(value)
457+
raise ValueError('Passed item and index have different timezone')
458+
451459
def _maybe_update_attributes(self, attrs):
452460
""" Update Index attributes (e.g. freq) depending on op """
453461
freq = attrs.get('freq', None)
@@ -1104,6 +1112,9 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
11041112
else:
11051113
raise
11061114

1115+
# --------------------------------------------------------------------
1116+
# Wrapping DatetimeArray
1117+
11071118
year = wrap_field_accessor(DatetimeArrayMixin.year)
11081119
month = wrap_field_accessor(DatetimeArrayMixin.month)
11091120
day = wrap_field_accessor(DatetimeArrayMixin.day)
@@ -1142,6 +1153,8 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
11421153
month_name = wrap_array_method(DatetimeArrayMixin.month_name, True)
11431154
day_name = wrap_array_method(DatetimeArrayMixin.day_name, True)
11441155

1156+
# --------------------------------------------------------------------
1157+
11451158
@Substitution(klass='DatetimeIndex')
11461159
@Appender(_shared_docs['searchsorted'])
11471160
def searchsorted(self, value, side='left', sorter=None):

pandas/core/indexes/timedeltas.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
209209
result._reset_identity()
210210
return result
211211

212-
_shallow_copy = Index._shallow_copy
213-
214212
@property
215213
def _formatter_func(self):
216214
from pandas.io.formats.format import _get_format_timedelta64
@@ -243,13 +241,18 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
243241
nat_rep=na_rep,
244242
justify='all').get_result()
245243

244+
# -------------------------------------------------------------------
245+
# Wrapping TimedeltaArray
246+
246247
days = wrap_field_accessor(TimedeltaArrayMixin.days)
247248
seconds = wrap_field_accessor(TimedeltaArrayMixin.seconds)
248249
microseconds = wrap_field_accessor(TimedeltaArrayMixin.microseconds)
249250
nanoseconds = wrap_field_accessor(TimedeltaArrayMixin.nanoseconds)
250251

251252
total_seconds = wrap_array_method(TimedeltaArrayMixin.total_seconds, True)
252253

254+
# -------------------------------------------------------------------
255+
253256
@Appender(_index_shared_docs['astype'])
254257
def astype(self, dtype, copy=True):
255258
dtype = pandas_dtype(dtype)

0 commit comments

Comments
 (0)