Skip to content

Commit 635b267

Browse files
committed
clusterfork
1 parent decc8ce commit 635b267

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ def _add_nat(self):
10181018
# and datetime dtypes
10191019
result = np.zeros(len(self), dtype=np.int64)
10201020
result.fill(iNaT)
1021-
return type(self)(result, dtype=self.dtype, freq=None)
1021+
return type(self)._simple_new(result, dtype=self.dtype, freq=None)
10221022

10231023
def _sub_nat(self):
10241024
"""

pandas/core/arrays/datetimes.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def wrapper(self, other):
141141
else:
142142
if isinstance(other, list):
143143
try:
144-
other = type(self)._from_sequence(other)
144+
other = type(self)._from_sequence_dti(other)
145145
except ValueError:
146146
other = np.array(other, dtype=np.object_)
147147
elif not isinstance(other, (np.ndarray, ABCIndexClass, ABCSeries,
@@ -173,7 +173,7 @@ def wrapper(self, other):
173173
not hasattr(other, 'asi8')):
174174
# e.g. other.dtype == 'datetime64[s]'
175175
# or an object-dtype ndarray
176-
other = type(self)._from_sequence(other)
176+
other = type(self)._from_sequence_dti(other)
177177

178178
result = op(self.view('i8'), other.view('i8'))
179179
o_mask = other._isnan
@@ -327,9 +327,13 @@ def _simple_new(cls, values, freq=None, dtype=None):
327327
return cls(values, freq=freq, dtype=dtype)
328328

329329
@classmethod
330-
def _from_sequence(cls, data, dtype=None, copy=False,
331-
tz=None, freq=None,
332-
dayfirst=False, yearfirst=False, ambiguous='raise'):
330+
def _from_sequence(cls, scalars, dtype=None, copy=False):
331+
assert all(isinstance(x, Timestamp) for x in scalars)
332+
333+
@classmethod
334+
def _from_sequence_dti(cls, data, dtype=None, copy=False,
335+
tz=None, freq=None,
336+
dayfirst=False, yearfirst=False, ambiguous='raise'):
333337

334338
freq, freq_infer = dtl.maybe_infer_freq(freq)
335339

@@ -735,7 +739,9 @@ def _add_delta(self, delta):
735739
result : DatetimeArray
736740
"""
737741
new_values = super(DatetimeArray, self)._add_delta(delta)
738-
return type(self)._from_sequence(new_values, tz=self.tz, freq='infer')
742+
result = type(self)(new_values, dtype=self.dtype)
743+
result._freq = result.inferred_freq
744+
return result
739745

740746
# -----------------------------------------------------------------
741747
# Timezone Conversion and Localization Methods
@@ -1074,8 +1080,8 @@ def normalize(self):
10741080
new_values[not_null] = new_values[not_null] - adjustment
10751081
else:
10761082
new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz)
1077-
return type(self)._from_sequence(new_values,
1078-
freq='infer').tz_localize(self.tz)
1083+
result = type(self)._from_sequence_dti(new_values, freq='infer')
1084+
resutn result.tz_localize(self.tz)
10791085

10801086
def to_period(self, freq=None):
10811087
"""
@@ -1664,6 +1670,7 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
16641670
"""
16651671

16661672
inferred_freq = None
1673+
inferred_tz = None
16671674

16681675
dtype = _validate_dt64_dtype(dtype)
16691676

@@ -1712,9 +1719,18 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
17121719
tz = maybe_infer_tz(tz, data.tz)
17131720
result = data._data
17141721

1715-
elif is_datetime64_dtype(data):
1722+
elif inferred_tz is not None:
1723+
# data returned from objects_to_datetime64ns represenet unix#
1724+
# timestamps
1725+
assert data.dtype == 'i8', data.dtype
1726+
result = data.view(_NS_DTYPE)
1727+
1728+
elif is_datetime64_dtype(data) or data.dtype == 'i8':
17161729
# tz-naive DatetimeArray or ndarray[datetime64]
17171730
data = getattr(data, "_data", data)
1731+
if data.dtype == 'i8':
1732+
data = data.view(_NS_DTYPE)
1733+
17181734
if data.dtype != _NS_DTYPE:
17191735
data = conversion.ensure_datetime64ns(data)
17201736

0 commit comments

Comments
 (0)