Skip to content
Open
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
19 changes: 14 additions & 5 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
if isinstance(data, (np.ndarray, ABCSeries)):
if issubclass(data.dtype.type, np.datetime64):
from pandas.tseries.index import DatetimeIndex
result = DatetimeIndex(data, copy=copy, name=name, **kwargs)
if dtype is not None and _o_dtype == dtype:
return Index(result.to_pydatetime(), dtype=_o_dtype)
try:
result = DatetimeIndex(data, copy=copy, name=name,
**kwargs)
except tslib.OutOfBoundsDatetime:
pass
else:
return result
if dtype is not None and _o_dtype == dtype:
return Index(result.to_pydatetime(), dtype=_o_dtype)
else:
return result
elif issubclass(data.dtype.type, np.timedelta64):
return Int64Index(data, copy=copy, name=name)

Expand Down Expand Up @@ -154,7 +159,11 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
if (inferred.startswith('datetime') or
tslib.is_timestamp_array(subarr)):
from pandas.tseries.index import DatetimeIndex
return DatetimeIndex(data, copy=copy, name=name, **kwargs)
try:
return DatetimeIndex(data, copy=copy, name=name,
**kwargs)
except (tslib.OutOfBoundsDatetime, ValueError):
pass
elif inferred == 'period':
return PeriodIndex(subarr, name=name, **kwargs)

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ def test_constructor_from_series(self):
result = pd.infer_freq(df['date'])
self.assertEqual(result,'MS')

def test_constructor_out_of_bounds_datetime(self):
expected = np.array([datetime(1000, 1, 1)])
self.assert_numpy_array_equal(expected, Index(expected).values)
expected_list = [datetime(1000, 1, 1)]
self.assert_numpy_array_equal(expected, Index(expected_list).values)

_skip_if_need_numpy_1_7()
expected = np.array([np.datetime64(datetime(1000, 1, 1))])
self.assert_numpy_array_equal(expected, Index(expected).values)
expected_list = [np.datetime64(datetime(1000, 1, 1))]
print(expected.shape, Index(expected_list).values.shape)
print(expected.dtype, Index(expected_list).values.dtype)
self.assert_numpy_array_equal(expected, Index(expected_list).values)

def test_index_ctor_infer_periodindex(self):
from pandas import period_range, PeriodIndex
xp = period_range('2012-1-1', freq='M', periods=3)
Expand Down