diff --git a/pandas/core/index.py b/pandas/core/index.py index c16e2eff06904..559d6611124cf 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -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) @@ -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) diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 3e578a5e36bb1..c3275cdd96de2 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -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)