Ejemplo n.º 1
0
    def _shallow_copy(self, values=None, **kwargs):
        # TODO: simplify, figure out type of values
        if values is None:
            values = self._data

        if isinstance(values, type(self)):
            values = values._values

        if not isinstance(values, PeriodArray):
            if (isinstance(values, np.ndarray) and
                    is_integer_dtype(values.dtype)):
                values = PeriodArray(values, freq=self.freq)
            else:
                # in particular, I would like to avoid period_array here.
                # Some people seem to be calling use with unexpected types
                # Index.difference -> ndarray[Period]
                # DatetimelikeIndexOpsMixin.repeat -> ndarray[ordinal]
                # I think that once all of Datetime* are EAs, we can simplify
                # this quite a bit.
                values = period_array(values, freq=self.freq)

        # I don't like overloading shallow_copy with freq changes.
        # See if it's used anywhere outside of test_resample_empty_dataframe
        attributes = self._get_attributes_dict()
        freq = kwargs.pop("freq", None)
        if freq:
            values = values.asfreq(freq)
            attributes.pop("freq", None)

        attributes.update(kwargs)
        if not len(values) and 'dtype' not in kwargs:
            attributes['dtype'] = self.dtype
        return self._simple_new(values, **attributes)
Ejemplo n.º 2
0
    def __new__(
        cls,
        data=None,
        ordinal=None,
        freq=None,
        dtype: Dtype | None = None,
        copy: bool = False,
        name: Hashable = None,
        **fields,
    ) -> PeriodIndex:

        valid_field_set = {
            "year",
            "month",
            "day",
            "quarter",
            "hour",
            "minute",
            "second",
        }

        if not set(fields).issubset(valid_field_set):
            argument = list(set(fields) - valid_field_set)[0]
            raise TypeError(
                f"__new__() got an unexpected keyword argument {argument}")

        name = maybe_extract_name(name, data, cls)

        if data is None and ordinal is None:
            # range-based.
            data, freq2 = PeriodArray._generate_range(None, None, None, freq,
                                                      fields)
            # PeriodArray._generate range does validation that fields is
            # empty when really using the range-based constructor.
            freq = freq2

            data = PeriodArray(data, freq=freq)
        else:
            freq = validate_dtype_freq(dtype, freq)

            # PeriodIndex allow PeriodIndex(period_index, freq=different)
            # Let's not encourage that kind of behavior in PeriodArray.

            if freq and isinstance(data, cls) and data.freq != freq:
                # TODO: We can do some of these with no-copy / coercion?
                # e.g. D -> 2D seems to be OK
                data = data.asfreq(freq)

            if data is None and ordinal is not None:
                # we strangely ignore `ordinal` if data is passed.
                ordinal = np.asarray(ordinal, dtype=np.int64)
                data = PeriodArray(ordinal, freq=freq)
            else:
                # don't pass copy here, since we copy later.
                data = period_array(data=data, freq=freq)

        if copy:
            data = data.copy()

        return cls._simple_new(data, name=name)
Ejemplo n.º 3
0
    def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
                periods=None, tz=None, dtype=None, copy=False, name=None,
                **fields):

        valid_field_set = {'year', 'month', 'day', 'quarter',
                           'hour', 'minute', 'second'}

        if not set(fields).issubset(valid_field_set):
            raise TypeError('__new__() got an unexpected keyword argument {}'.
                            format(list(set(fields) - valid_field_set)[0]))

        if name is None and hasattr(data, 'name'):
            name = data.name

        if data is None and ordinal is None:
            # range-based.
            if periods is not None:
                if is_float(periods):
                    periods = int(periods)

                elif not is_integer(periods):
                    msg = 'periods must be a number, got {periods}'
                    raise TypeError(msg.format(periods=periods))

            data, freq = PeriodArray._generate_range(start, end, periods,
                                                     freq, fields)
            data = PeriodArray(data, freq=freq)
        else:
            if freq is None and dtype is not None:
                freq = PeriodDtype(dtype).freq
            elif freq and dtype:
                freq = PeriodDtype(freq).freq
                dtype = PeriodDtype(dtype).freq

                if freq != dtype:
                    msg = "specified freq and dtype are different"
                    raise IncompatibleFrequency(msg)

            # PeriodIndex allow PeriodIndex(period_index, freq=different)
            # Let's not encourage that kind of behavior in PeriodArray.

            if freq and isinstance(data, cls) and data.freq != freq:
                # TODO: We can do some of these with no-copy / coercion?
                # e.g. D -> 2D seems to be OK
                data = data.asfreq(freq)

            if data is None and ordinal is not None:
                # we strangely ignore `ordinal` if data is passed.
                ordinal = np.asarray(ordinal, dtype=np.int64)
                data = PeriodArray(ordinal, freq)
            else:
                # don't pass copy here, since we copy later.
                data = period_array(data=data, freq=freq)

        if copy:
            data = data.copy()

        return cls._simple_new(data, name=name)
Ejemplo n.º 4
0
    def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
                periods=None, tz=None, dtype=None, copy=False, name=None,
                **fields):

        valid_field_set = {'year', 'month', 'day', 'quarter',
                           'hour', 'minute', 'second'}

        if not set(fields).issubset(valid_field_set):
            raise TypeError('__new__() got an unexpected keyword argument {}'.
                            format(list(set(fields) - valid_field_set)[0]))

        if name is None and hasattr(data, 'name'):
            name = data.name

        if data is None and ordinal is None:
            # range-based.
            data, freq = PeriodArray._generate_range(start, end, periods,
                                                     freq, fields)
            data = PeriodArray(data, freq=freq)
        else:
            freq = dtl.validate_dtype_freq(dtype, freq)

            # PeriodIndex allow PeriodIndex(period_index, freq=different)
            # Let's not encourage that kind of behavior in PeriodArray.

            if freq and isinstance(data, cls) and data.freq != freq:
                # TODO: We can do some of these with no-copy / coercion?
                # e.g. D -> 2D seems to be OK
                data = data.asfreq(freq)

            if data is None and ordinal is not None:
                # we strangely ignore `ordinal` if data is passed.
                ordinal = np.asarray(ordinal, dtype=np.int64)
                data = PeriodArray(ordinal, freq)
            else:
                # don't pass copy here, since we copy later.
                data = period_array(data=data, freq=freq)

        if copy:
            data = data.copy()

        return cls._simple_new(data, name=name)
Ejemplo n.º 5
0
    def __new__(cls,
                data=None,
                ordinal=None,
                freq=None,
                start=None,
                end=None,
                periods=None,
                tz=None,
                dtype=None,
                copy=False,
                name=None,
                **fields):

        valid_field_set = {
            "year",
            "month",
            "day",
            "quarter",
            "hour",
            "minute",
            "second",
        }

        if not set(fields).issubset(valid_field_set):
            raise TypeError(
                "__new__() got an unexpected keyword argument {}".format(
                    list(set(fields) - valid_field_set)[0]))

        if name is None and hasattr(data, "name"):
            name = data.name

        if data is None and ordinal is None:
            # range-based.
            data, freq2 = PeriodArray._generate_range(start, end, periods,
                                                      freq, fields)
            # PeriodArray._generate range does validate that fields is
            # empty when really using the range-based constructor.
            if not fields:
                msg = ("Creating a PeriodIndex by passing range "
                       "endpoints is deprecated.  Use "
                       "`pandas.period_range` instead.")
                # period_range differs from PeriodIndex for cases like
                # start="2000", periods=4
                # PeriodIndex interprets that as A-DEC freq.
                # period_range interprets it as 'D' freq.
                cond = freq is None and (
                    (start and not isinstance(start, Period)) or
                    (end and not isinstance(end, Period)))
                if cond:
                    msg += (" Note that the default `freq` may differ. Pass "
                            "'freq=\"{}\"' to ensure the same output.").format(
                                freq2.freqstr)
                warnings.warn(msg, FutureWarning, stacklevel=2)
            freq = freq2

            data = PeriodArray(data, freq=freq)
        else:
            freq = validate_dtype_freq(dtype, freq)

            # PeriodIndex allow PeriodIndex(period_index, freq=different)
            # Let's not encourage that kind of behavior in PeriodArray.

            if freq and isinstance(data, cls) and data.freq != freq:
                # TODO: We can do some of these with no-copy / coercion?
                # e.g. D -> 2D seems to be OK
                data = data.asfreq(freq)

            if data is None and ordinal is not None:
                # we strangely ignore `ordinal` if data is passed.
                ordinal = np.asarray(ordinal, dtype=np.int64)
                data = PeriodArray(ordinal, freq)
            else:
                # don't pass copy here, since we copy later.
                data = period_array(data=data, freq=freq)

        if copy:
            data = data.copy()

        return cls._simple_new(data, name=name)
Ejemplo n.º 6
0
    def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
                periods=None, tz=None, dtype=None, copy=False, name=None,
                **fields):

        valid_field_set = {'year', 'month', 'day', 'quarter',
                           'hour', 'minute', 'second'}

        if not set(fields).issubset(valid_field_set):
            raise TypeError('__new__() got an unexpected keyword argument {}'.
                            format(list(set(fields) - valid_field_set)[0]))

        if name is None and hasattr(data, 'name'):
            name = data.name

        if data is None and ordinal is None:
            # range-based.
            data, freq2 = PeriodArray._generate_range(start, end, periods,
                                                      freq, fields)
            # PeriodArray._generate range does validate that fields is
            # empty when really using the range-based constructor.
            if not fields:
                msg = ("Creating a PeriodIndex by passing range "
                       "endpoints is deprecated.  Use "
                       "`pandas.period_range` instead.")
                # period_range differs from PeriodIndex for cases like
                # start="2000", periods=4
                # PeriodIndex interprets that as A-DEC freq.
                # period_range interprets it as 'D' freq.
                cond = (
                    freq is None and (
                        (start and not isinstance(start, Period)) or
                        (end and not isinstance(end, Period))
                    )
                )
                if cond:
                    msg += (
                        " Note that the default `freq` may differ. Pass "
                        "'freq=\"{}\"' to ensure the same output."
                    ).format(freq2.freqstr)
                warnings.warn(msg, FutureWarning, stacklevel=2)
            freq = freq2

            data = PeriodArray(data, freq=freq)
        else:
            freq = validate_dtype_freq(dtype, freq)

            # PeriodIndex allow PeriodIndex(period_index, freq=different)
            # Let's not encourage that kind of behavior in PeriodArray.

            if freq and isinstance(data, cls) and data.freq != freq:
                # TODO: We can do some of these with no-copy / coercion?
                # e.g. D -> 2D seems to be OK
                data = data.asfreq(freq)

            if data is None and ordinal is not None:
                # we strangely ignore `ordinal` if data is passed.
                ordinal = np.asarray(ordinal, dtype=np.int64)
                data = PeriodArray(ordinal, freq)
            else:
                # don't pass copy here, since we copy later.
                data = period_array(data=data, freq=freq)

        if copy:
            data = data.copy()

        return cls._simple_new(data, name=name)