Пример #1
0
    def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
        """
        Cast to DatetimeArray/Index.

        Parameters
        ----------
        freq : str or DateOffset, optional
            Target frequency. The default is 'D' for week or longer,
            'S' otherwise.
        how : {'s', 'e', 'start', 'end'}
            Whether to use the start or end of the time period being converted.

        Returns
        -------
        DatetimeArray/Index
        """
        from pandas.core.arrays import DatetimeArray

        how = libperiod.validate_end_alias(how)

        end = how == "E"
        if end:
            if freq == "B" or self.freq == "B":
                # roll forward to ensure we land on B date
                adjust = Timedelta(1, "D") - Timedelta(1, "ns")
                return self.to_timestamp(how="start") + adjust
            else:
                adjust = Timedelta(1, "ns")
                return (self + self.freq).to_timestamp(how="start") - adjust

        if freq is None:
            freq = self._dtype._get_to_timestamp_base()
            base = freq
        else:
            freq = Period._maybe_convert_freq(freq)
            base = freq._period_dtype_code

        new_parr = self.asfreq(freq, how=how)

        new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
        dta = DatetimeArray(new_data)

        if self.freq.name == "B":
            # See if we can retain BDay instead of Day in cases where
            #  len(self) is too small for infer_freq to distinguish between them
            diffs = libalgos.unique_deltas(self.asi8)
            if len(diffs) == 1:
                diff = diffs[0]
                if diff == self.freq.n:
                    dta._freq = self.freq
                elif diff == 1:
                    dta._freq = self.freq.base
                # TODO: other cases?
            return dta
        else:
            return dta._with_freq("infer")
Пример #2
0
    def _shallow_copy(self, values, name: Hashable = no_default):
        name = self.name if name is no_default else name

        if values.dtype.kind == "f":
            return Float64Index(values, name=name)
        # GH 46675 & 43885: If values is equally spaced, return a
        # more memory-compact RangeIndex instead of Int64Index
        unique_diffs = unique_deltas(values)
        if len(unique_diffs) == 1 and unique_diffs[0] != 0:
            diff = unique_diffs[0]
            new_range = range(values[0], values[-1] + diff, diff)
            return type(self)._simple_new(new_range, name=name)
        else:
            return Int64Index._simple_new(values, name=name)
Пример #3
0
    def _concat_same_dtype(self, to_concat, name):
        """
        Concatenate to_concat which has the same class.
        """

        # do not pass tz to set because tzlocal cannot be hashed
        if len({str(x.dtype) for x in to_concat}) != 1:
            raise ValueError("to_concat must have the same tz")

        new_data = type(self._data)._concat_same_type(to_concat)

        if not is_period_dtype(self.dtype):
            # GH 3232: If the concat result is evenly spaced, we can retain the
            # original frequency
            is_diff_evenly_spaced = len(unique_deltas(new_data.asi8)) == 1
            if is_diff_evenly_spaced:
                new_data._freq = self.freq

        return self._simple_new(new_data, name=name)
Пример #4
0
    def _concat_same_dtype(self, to_concat, name):
        """
        Concatenate to_concat which has the same class.
        """
        attribs = self._get_attributes_dict()
        attribs["name"] = name
        # do not pass tz to set because tzlocal cannot be hashed
        if len({str(x.dtype) for x in to_concat}) != 1:
            raise ValueError("to_concat must have the same tz")

        new_data = type(self._values)._concat_same_type(to_concat).asi8

        # GH 3232: If the concat result is evenly spaced, we can retain the
        # original frequency
        is_diff_evenly_spaced = len(unique_deltas(new_data)) == 1
        if not is_period_dtype(self) and not is_diff_evenly_spaced:
            # reset freq
            attribs["freq"] = None

        return self._simple_new(new_data, **attribs)
Пример #5
0
    def _concat_same_dtype(self, to_concat, name):
        """
        Concatenate to_concat which has the same class.
        """
        attribs = self._get_attributes_dict()
        attribs['name'] = name
        # do not pass tz to set because tzlocal cannot be hashed
        if len({str(x.dtype) for x in to_concat}) != 1:
            raise ValueError('to_concat must have the same tz')

        new_data = type(self._values)._concat_same_type(to_concat).asi8

        # GH 3232: If the concat result is evenly spaced, we can retain the
        # original frequency
        is_diff_evenly_spaced = len(unique_deltas(new_data)) == 1
        if not is_period_dtype(self) and not is_diff_evenly_spaced:
            # reset freq
            attribs['freq'] = None

        return self._simple_new(new_data, **attribs)
Пример #6
0
 def ydiffs(self):
     return unique_deltas(self.fields['Y'].astype('i8'))
Пример #7
0
 def ydiffs(self):
     return unique_deltas(self.fields["Y"].astype("i8"))
Пример #8
0
 def mdiffs(self):
     nmonths = self.fields["Y"] * 12 + self.fields["M"]
     return unique_deltas(nmonths.astype("i8"))
Пример #9
0
 def deltas_asi8(self):
     # NB: we cannot use self.i8values here because we may have converted
     #  the tz in __init__
     return unique_deltas(self.index.asi8)
Пример #10
0
 def deltas(self):
     return unique_deltas(self.i8values)
Пример #11
0
 def mdiffs(self) -> npt.NDArray[np.int64]:
     nmonths = self.fields["Y"] * 12 + self.fields["M"]
     return unique_deltas(nmonths.astype("i8"))
Пример #12
0
 def ydiffs(self) -> npt.NDArray[np.int64]:
     return unique_deltas(self.fields["Y"].astype("i8"))
Пример #13
0
 def deltas_asi8(self):
     return unique_deltas(self.index.asi8)
Пример #14
0
 def deltas(self) -> npt.NDArray[np.int64]:
     return unique_deltas(self.i8values)
Пример #15
0
 def mdiffs(self):
     nmonths = self.fields['Y'] * 12 + self.fields['M']
     return unique_deltas(nmonths.astype('i8'))
Пример #16
0
 def deltas_asi8(self):
     return unique_deltas(self.index.asi8)
Пример #17
0
 def deltas(self):
     return unique_deltas(self.values)
Пример #18
0
 def mdiffs(self):
     nmonths = self.fields['Y'] * 12 + self.fields['M']
     return unique_deltas(nmonths.astype('i8'))
Пример #19
0
 def ydiffs(self):
     return unique_deltas(self.fields['Y'].astype('i8'))