Skip to content

Commit 36ef227

Browse files
committed
ENH: convert to TimeSeries when setting a DatetimeIndex. close #2139
1 parent 7679eee commit 36ef227

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

pandas/core/series.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
368368

369369
return subarr
370370

371+
def _make_time_series(self):
372+
# oh boy #2139
373+
self.__class__ = TimeSeries
374+
371375
@classmethod
372376
def from_array(cls, arr, index=None, name=None, copy=False):
373377
"""

pandas/sparse/series.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ def __new__(cls, data, index=None, sparse_index=None, kind='block',
155155
output.name = name
156156
return output
157157

158+
def _make_time_series(self):
159+
# oh boy #2139
160+
self.__class__ = SparseTimeSeries
161+
158162
@classmethod
159163
def from_array(cls, arr, index=None, name=None, copy=False,fill_value=None):
160164
"""

pandas/src/properties.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ cdef class SeriesIndex(object):
5757
def __set__(self, obj, value):
5858
if len(obj) != len(value):
5959
raise AssertionError('Index length did not match values')
60-
obj._index = self._check_type(value)
60+
obj._index = val = self._check_type(value)
61+
if hasattr(val, 'tz'):
62+
# hack for #2139
63+
obj._make_time_series()
64+
6165

6266
cdef class ValuesProperty(object):
6367

pandas/tests/test_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,6 +3345,14 @@ def test_reset_index(self):
33453345
self.assert_(rs.index.equals(Index(index.get_level_values(1))))
33463346
self.assert_(isinstance(rs, Series))
33473347

3348+
def test_set_index_makes_timeseries(self):
3349+
idx = tm.makeDateIndex(10)
3350+
3351+
s = Series(range(10))
3352+
s.index = idx
3353+
3354+
self.assertTrue(isinstance(s, TimeSeries))
3355+
33483356
def test_timeseries_coercion(self):
33493357
idx = tm.makeDateIndex(10000)
33503358
ser = Series(np.random.randn(len(idx)), idx.astype(object))

0 commit comments

Comments
 (0)