@deprecate_kwarg(old_arg_name='infer_dst', new_arg_name='ambiguous',
                     mapping={True: 'infer', False: 'raise'})
    def __new__(cls, data=None,
                freq=None, start=None, end=None, periods=None,
                copy=False, name=None, tz=None,
                verify_integrity=True, normalize=False,
                closed=None, ambiguous='raise', dtype=None, **kwargs):

        dayfirst = kwargs.pop('dayfirst', None)
        yearfirst = kwargs.pop('yearfirst', None)

        freq_infer = False
        if not isinstance(freq, DateOffset):

            # if a passed freq is None, don't infer automatically
            if freq != 'infer':
                freq = to_offset(freq)
            else:
                freq_infer = True
                freq = None

        if periods is not None:
            if is_float(periods):
                periods = int(periods)
            elif not is_integer(periods):
                raise ValueError('Periods must be a number, got %s' %
                                 str(periods))

        if data is None and freq is None:
            raise ValueError("Must provide freq argument if no data is "
                             "supplied")

        # if dtype has an embeded tz, capture it
        if dtype is not None:
            try:
                dtype = DatetimeTZDtype.construct_from_string(dtype)
                dtz = getattr(dtype, 'tz', None)
                if dtz is not None:
                    if tz is not None and str(tz) != str(dtz):
                        raise ValueError("cannot supply both a tz and a dtype"
                                         " with a tz")
                    tz = dtz
            except __HOLE__:
                pass

        if data is None:
            return cls._generate(start, end, periods, name, freq,
                                 tz=tz, normalize=normalize, closed=closed,
                                 ambiguous=ambiguous)

        if not isinstance(data, (np.ndarray, Index, ABCSeries)):
            if lib.isscalar(data):
                raise ValueError('DatetimeIndex() must be called with a '
                                 'collection of some kind, %s was passed'
                                 % repr(data))

            # other iterable of some kind
            if not isinstance(data, (list, tuple)):
                data = list(data)

            data = np.asarray(data, dtype='O')

            # try a few ways to make it datetime64
            if lib.is_string_array(data):
                data = tslib.parse_str_array_to_datetime(data, freq=freq,
                                                         dayfirst=dayfirst,
                                                         yearfirst=yearfirst)
            else:
                data = tools.to_datetime(data, errors='raise')
                data.offset = freq
                if isinstance(data, DatetimeIndex):
                    if name is not None:
                        data.name = name

                    if tz is not None:

                        # we might already be localized to this tz
                        # so passing the same tz is ok
                        # however any other tz is a no-no
                        if data.tz is None:
                            return data.tz_localize(tz, ambiguous=ambiguous)
                        elif str(tz) != str(data.tz):
                            raise TypeError("Already tz-aware, use tz_convert "
                                            "to convert.")

                    return data

        if issubclass(data.dtype.type, compat.string_types):
            data = tslib.parse_str_array_to_datetime(data, freq=freq,
                                                     dayfirst=dayfirst,
                                                     yearfirst=yearfirst)

        if issubclass(data.dtype.type, np.datetime64) or is_datetimetz(data):
            if isinstance(data, ABCSeries):
                data = data._values
            if isinstance(data, DatetimeIndex):
                if tz is None:
                    tz = data.tz

                else:
                    # the tz's must match
                    if str(tz) != str(data.tz):
                        raise TypeError("Already tz-aware, use tz_convert "
                                        "to convert.")

                subarr = data.values

                if freq is None:
                    freq = data.offset
                    verify_integrity = False
            else:
                if data.dtype != _NS_DTYPE:
                    subarr = tslib.cast_to_nanoseconds(data)
                else:
                    subarr = data
        elif data.dtype == _INT64_DTYPE:
            if isinstance(data, Int64Index):
                raise TypeError('cannot convert Int64Index->DatetimeIndex')
            if copy:
                subarr = np.asarray(data, dtype=_NS_DTYPE)
            else:
                subarr = data.view(_NS_DTYPE)
        else:
            if isinstance(data, (ABCSeries, Index)):
                values = data._values
            else:
                values = data

            if lib.is_string_array(values):
                subarr = tslib.parse_str_array_to_datetime(
                    values, freq=freq, dayfirst=dayfirst, yearfirst=yearfirst)
            else:
                try:
                    subarr = tools.to_datetime(data, box=False)

                    # make sure that we have a index/ndarray like (and not a
                    # Series)
                    if isinstance(subarr, ABCSeries):
                        subarr = subarr._values
                        if subarr.dtype == np.object_:
                            subarr = tools._to_datetime(subarr, box=False)

                except ValueError:
                    # tz aware
                    subarr = tools._to_datetime(data, box=False, utc=True)

                # we may not have been able to convert
                if not (is_datetimetz(subarr) or
                        np.issubdtype(subarr.dtype, np.datetime64)):
                    raise ValueError('Unable to convert %s to datetime dtype'
                                     % str(data))

        if isinstance(subarr, DatetimeIndex):
            if tz is None:
                tz = subarr.tz
        else:
            if tz is not None:
                tz = tslib.maybe_get_tz(tz)

                if (not isinstance(data, DatetimeIndex) or
                        getattr(data, 'tz', None) is None):
                    # Convert tz-naive to UTC
                    ints = subarr.view('i8')
                    subarr = tslib.tz_localize_to_utc(ints, tz,
                                                      ambiguous=ambiguous)

                subarr = subarr.view(_NS_DTYPE)

        subarr = cls._simple_new(subarr, name=name, freq=freq, tz=tz)

        # if dtype is provided, coerce here
        if dtype is not None:

            if not is_dtype_equal(subarr.dtype, dtype):

                if subarr.tz is not None:
                    raise ValueError("cannot localize from non-UTC data")
                dtype = DatetimeTZDtype.construct_from_string(dtype)
                subarr = subarr.tz_localize(dtype.tz)

        if verify_integrity and len(subarr) > 0:
            if freq is not None and not freq_infer:
                inferred = subarr.inferred_freq
                if inferred != freq.freqstr:
                    on_freq = cls._generate(subarr[0], None, len(
                        subarr), None, freq, tz=tz, ambiguous=ambiguous)
                    if not np.array_equal(subarr.asi8, on_freq.asi8):
                        raise ValueError('Inferred frequency {0} from passed '
                                         'dates does not conform to passed '
                                         'frequency {1}'
                                         .format(inferred, freq.freqstr))

        if freq_infer:
            inferred = subarr.inferred_freq
            if inferred:
                subarr.offset = to_offset(inferred)

        return subarr
dataset/ETHPy150Open pydata/pandas/pandas/tseries/index.py/DatetimeIndex.__new__