Exemple #1
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError('Partial indexing only valid for '
                             'ordered time series')

        asdt, parsed, reso = parse_time_string(key, self.freq)
        key = asdt

        if reso == 'year':
            t1 = Period(year=parsed.year, freq='A')
        elif reso == 'month':
            t1 = Period(year=parsed.year, motnh=parsed.month, freq='M')
        elif reso == 'quarter':
            q = (parsed.month - 1) // 4 + 1
            t1 = Period(year=parsed.year, quarter=q, freq='Q-DEC')
        else:
            raise KeyError(key)

        ordinals = self.values

        t2 = t1.asfreq(self.freq, how='end')
        t1 = t1.asfreq(self.freq, how='start')

        left = ordinals.searchsorted(t1.ordinal, side='left')
        right = ordinals.searchsorted(t2.ordinal, side='right')
        return slice(left, right)
Exemple #2
0
    def get_value(self, series, key):
        """
        Fast lookup of value from 1-dimensional ndarray. Only use this if you
        know what you're doing
        """
        try:
            return super(PeriodIndex, self).get_value(series, key)
        except KeyError:
            try:
                asdt, parsed, reso = parse_time_string(key)
                grp = _freq_mod._infer_period_group(reso)
                freqn = _freq_mod._period_group(self.freq)

                # if our data is higher resolution than requested key, slice
                if grp < freqn:
                    iv = Period(asdt, freq=(grp,1))
                    ord1 = iv.asfreq(self.freq, how='S').ordinal
                    ord2 = iv.asfreq(self.freq, how='E').ordinal
                    pos = np.searchsorted(self.values, [ord1, ord2])
                    key = slice(pos[0], pos[1]+1)
                    return series[key]
                else:
                    key = to_period(asdt, freq=self.freq).ordinal
                    return self._engine.get_value(series, key)
            except TypeError:
                pass
            except KeyError:
                pass
            except IndexError:
                ival = Period(key, freq=self.freq)
                raise IndexError("%s is out of bounds" % ival)

            key = to_period(key, self.freq).ordinal
            return self._engine.get_value(series, key)
Exemple #3
0
def _get_date_and_freq(value, freq):
    value = value.upper()
    dt, _, reso = parse_time_string(value, freq)

    if freq is None:
        if reso == 'year':
            freq = 'A'
        elif reso == 'quarter':
            freq = 'Q'
        elif reso == 'month':
            freq = 'M'
        elif reso == 'day':
            freq = 'D'
        elif reso == 'hour':
            freq = 'H'
        elif reso == 'minute':
            freq = 'T'
        elif reso == 'second':
            freq = 'S'
        elif reso == 'microsecond':
            if dt.microsecond % 1000 == 0:
                freq = 'L'
            else:
                freq = 'U'
        else:
            raise ValueError("Invalid frequency or could not infer: %s" % reso)

    return dt, freq
Exemple #4
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError("Partial indexing only valid for " "ordered time series")

        asdt, parsed, reso = parse_time_string(key, self.freq)
        key = asdt

        if reso == "year":
            t1 = Period(year=parsed.year, freq="A")
        elif reso == "month":
            t1 = Period(year=parsed.year, month=parsed.month, freq="M")
        elif reso == "quarter":
            q = (parsed.month - 1) // 3 + 1
            t1 = Period(year=parsed.year, quarter=q, freq="Q-DEC")
        else:
            raise KeyError(key)

        ordinals = self.values

        t2 = t1.asfreq(self.freq, how="end")
        t1 = t1.asfreq(self.freq, how="start")

        left = ordinals.searchsorted(t1.ordinal, side="left")
        right = ordinals.searchsorted(t2.ordinal, side="right")
        return slice(left, right)
Exemple #5
0
    def get_loc(self, key, method=None):
        """
        Get integer location for requested label

        Returns
        -------
        loc : int
        """
        try:
            return self._engine.get_loc(key)
        except KeyError:
            if is_integer(key):
                raise

            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                key = asdt
            except TypeError:
                pass

            key = Period(key, self.freq)
            try:
                return Index.get_loc(self, key.ordinal, method=method)
            except KeyError:
                raise KeyError(key)
Exemple #6
0
    def test_parsers_dayfirst_yearfirst(self):
        # str : dayfirst, yearfirst, expected
        cases = {'10-11-12': [(False, False, datetime.datetime(2012, 10, 11)),
                              (True, False, datetime.datetime(2012, 11, 10)),
                              (False, True, datetime.datetime(2010, 11, 12)),
                              (True, True, datetime.datetime(2010, 11, 12))],
                 '20/12/21': [(False, False, datetime.datetime(2021, 12, 20)),
                              (True, False, datetime.datetime(2021, 12, 20)),
                              (False, True, datetime.datetime(2020, 12, 21)),
                              (True, True, datetime.datetime(2020, 12, 21))]}

        tm._skip_if_no_dateutil()
        from dateutil.parser import parse
        for date_str, values in compat.iteritems(cases):
            for dayfirst, yearfirst ,expected in values:
                result1, _, _ = tools.parse_time_string(date_str, dayfirst=dayfirst,
                                                        yearfirst=yearfirst)

                result2 = to_datetime(date_str, dayfirst=dayfirst,
                                      yearfirst=yearfirst)

                result3 = DatetimeIndex([date_str], dayfirst=dayfirst,
                                        yearfirst=yearfirst)[0]

                # Timestamp doesn't support dayfirst and yearfirst

                self.assertEqual(result1, expected)
                self.assertEqual(result2, expected)
                self.assertEqual(result3, expected)

                # compare with dateutil result
                dateutil_result = parse(date_str, dayfirst=dayfirst, yearfirst=yearfirst)
                self.assertEqual(dateutil_result, expected)
Exemple #7
0
 def _get_string_slice(self, key):
     freq = getattr(self, 'freqstr',
                    getattr(self, 'inferred_freq', None))
     asdt, parsed, reso = parse_time_string(key, freq)
     key = asdt
     loc = self._partial_date_slice(reso, parsed)
     return loc
Exemple #8
0
def _get_date_and_freq(value, freq):
    value = value.upper()
    dt, _, reso = parse_time_string(value, freq)

    if freq is None:
        if reso == "year":
            freq = "A"
        elif reso == "quarter":
            freq = "Q"
        elif reso == "month":
            freq = "M"
        elif reso == "day":
            freq = "D"
        elif reso == "hour":
            freq = "H"
        elif reso == "minute":
            freq = "T"
        elif reso == "second":
            freq = "S"
        elif reso == "microsecond":
            if dt.microsecond % 1000 == 0:
                freq = "L"
            else:
                freq = "U"
        else:
            raise ValueError("Invalid frequency or could not infer: %s" % reso)

    return dt, freq
Exemple #9
0
    def get_loc(self, key, method=None, tolerance=None):
        """
        Get integer location for requested label

        Returns
        -------
        loc : int
        """
        try:
            return self._engine.get_loc(key)
        except KeyError:
            if is_integer(key):
                raise

            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                key = asdt
            except TypeError:
                pass

            try:
                key = Period(key, freq=self.freq)
            except ValueError:
                # we cannot construct the Period
                # as we have an invalid type
                raise KeyError(key)

            try:
                ordinal = tslib.iNaT if key is tslib.NaT else key.ordinal
                return Index.get_loc(self, ordinal, method, tolerance)
            except KeyError:
                raise KeyError(key)
Exemple #10
0
    def _maybe_cast_slice_bound(self, label, side, kind):
        """
        If label is a string or a datetime, cast it to Period.ordinal according
        to resolution.

        Parameters
        ----------
        label : object
        side : {'left', 'right'}
        kind : {'ix', 'loc', 'getitem'}

        Returns
        -------
        bound : Period or object

        Notes
        -----
        Value of `side` parameter should be validated in caller.

        """
        assert kind in ['ix', 'loc', 'getitem']

        if isinstance(label, datetime):
            return Period(label, freq=self.freq)
        elif isinstance(label, compat.string_types):
            try:
                _, parsed, reso = parse_time_string(label, self.freq)
                bounds = self._parsed_string_to_bounds(reso, parsed)
                return bounds[0 if side == 'left' else 1]
            except Exception:
                raise KeyError(label)
        elif is_integer(label) or is_float(label):
            self._invalid_indexer('slice', label)

        return label
Exemple #11
0
    def get_value(self, series, key):
        """
        Fast lookup of value from 1-dimensional ndarray. Only use this if you
        know what you're doing
        """
        try:
            return Index.get_value(self, series, key)
        except KeyError:

            try:
                freq = getattr(self, "freq", getattr(self, "inferred_freq", None))
                asdt, parsed, reso = parse_time_string(key, freq)
                key = asdt
                loc = self._partial_date_slice(reso, parsed)
                return series[loc]
            except (TypeError, ValueError, KeyError):
                pass

            if isinstance(key, time):
                locs = self._indices_at_time(key)
                return series.take(locs)

            stamp = Timestamp(key)
            try:
                return self._engine.get_value(series, stamp)
            except KeyError:
                raise KeyError(stamp)
Exemple #12
0
    def test_parsers_monthfreq(self):
        cases = {"201101": datetime.datetime(2011, 1, 1, 0, 0), "200005": datetime.datetime(2000, 5, 1, 0, 0)}

        for date_str, expected in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str, freq="M")
            result2 = tools._to_datetime(date_str, freq="M")
            self.assertEqual(result1, expected)
            self.assertEqual(result2, expected)
Exemple #13
0
    def test_parsers_quarterly_with_freq(self):

        msg = 'Incorrect quarterly string is given, quarter must be between 1 and 4: 2013Q5'
        with tm.assertRaisesRegexp(tslib.DateParseError, msg):
            tools.parse_time_string('2013Q5')

        # GH 5418
        msg = 'Unable to retrieve month information from given freq: INVLD-L-DEC-SAT'
        with tm.assertRaisesRegexp(tslib.DateParseError, msg):
            tools.parse_time_string('2013Q1', freq='INVLD-L-DEC-SAT')

        cases = {('2013Q2', None): datetime.datetime(2013, 4, 1),
                 ('2013Q2', 'A-APR'): datetime.datetime(2012, 8, 1),
                 ('2013-Q2', 'A-DEC'): datetime.datetime(2013, 4, 1)}

        for (date_str, freq), exp in compat.iteritems(cases):
            result, _, _ = tools.parse_time_string(date_str, freq=freq)
            self.assertEqual(result, exp)
Exemple #14
0
    def test_parsers_quarterly_with_freq(self):
        msg = "Incorrect quarterly string is given, quarter " "must be between 1 and 4: 2013Q5"
        with tm.assertRaisesRegexp(tslib.DateParseError, msg):
            tools.parse_time_string("2013Q5")

        # GH 5418
        msg = "Unable to retrieve month information from given freq: " "INVLD-L-DEC-SAT"
        with tm.assertRaisesRegexp(tslib.DateParseError, msg):
            tools.parse_time_string("2013Q1", freq="INVLD-L-DEC-SAT")

        cases = {
            ("2013Q2", None): datetime.datetime(2013, 4, 1),
            ("2013Q2", "A-APR"): datetime.datetime(2012, 8, 1),
            ("2013-Q2", "A-DEC"): datetime.datetime(2013, 4, 1),
        }

        for (date_str, freq), exp in compat.iteritems(cases):
            result, _, _ = tools.parse_time_string(date_str, freq=freq)
            self.assertEqual(result, exp)
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError("Partial indexing only valid for " "ordered time series")

        key, parsed, reso = parse_time_string(key, self.freq)
        grp = frequencies.Resolution.get_freq_group(reso)
        freqn = frequencies.get_freq_group(self.freq)
        if reso in ["day", "hour", "minute", "second"] and not grp < freqn:
            raise KeyError(key)

        t1, t2 = self._parsed_string_to_bounds(reso, parsed)
        return slice(self.searchsorted(t1.ordinal, side="left"), self.searchsorted(t2.ordinal, side="right"))
Exemple #16
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError('Partial indexing only valid for '
                             'ordered time series')

        key, parsed, reso = parse_time_string(key, self.freq)
        grp = frequencies.Resolution.get_freq_group(reso)
        freqn = frequencies.get_freq_group(self.freq)
        if reso in ['day', 'hour', 'minute', 'second'] and not grp < freqn:
            raise KeyError(key)

        t1, t2 = self._parsed_string_to_bounds(reso, parsed)
        return slice(self.searchsorted(t1.ordinal, side='left'),
                     self.searchsorted(t2.ordinal, side='right'))
Exemple #17
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError('Partial indexing only valid for '
                             'ordered time series')

        key, parsed, reso = parse_time_string(key, self.freq)

        grp = frequencies._infer_period_group(reso)
        freqn = frequencies._period_group(self.freq)
        if reso in ['day', 'hour', 'minute', 'second'] and not grp < freqn:
            raise KeyError(key)

        t1, t2 = self._parsed_string_to_bounds(reso, parsed)
        return slice(self.searchsorted(t1.ordinal, side='left'),
                     self.searchsorted(t2.ordinal, side='right'))
Exemple #18
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError("Partial indexing only valid for " "ordered time series")

        key, parsed, reso = parse_time_string(key, self.freq)

        grp = _freq_mod._infer_period_group(reso)
        freqn = _freq_mod._period_group(self.freq)

        if reso == "year":
            t1 = Period(year=parsed.year, freq="A")
        elif reso == "month":
            t1 = Period(year=parsed.year, month=parsed.month, freq="M")
        elif reso == "quarter":
            q = (parsed.month - 1) // 3 + 1
            t1 = Period(year=parsed.year, quarter=q, freq="Q-DEC")
        elif reso == "day" and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day, freq="D")
        elif reso == "hour" and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day, hour=parsed.hour, freq="H")
        elif reso == "minute" and grp < freqn:
            t1 = Period(
                year=parsed.year, month=parsed.month, day=parsed.day, hour=parsed.hour, minute=parsed.minute, freq="T"
            )
        elif reso == "second" and grp < freqn:
            t1 = Period(
                year=parsed.year,
                month=parsed.month,
                day=parsed.day,
                hour=parsed.hour,
                minute=parsed.minute,
                second=parsed.second,
                freq="S",
            )
        else:
            raise KeyError(key)

        ordinals = self.values

        t2 = t1.asfreq(self.freq, how="end")
        t1 = t1.asfreq(self.freq, how="start")

        left = ordinals.searchsorted(t1.ordinal, side="left")
        right = ordinals.searchsorted(t2.ordinal, side="right")
        return slice(left, right)
Exemple #19
0
    def get_loc(self, key):
        """
        Get integer location for requested label

        Returns
        -------
        loc : int
        """
        try:
            return self._engine.get_loc(key)
        except KeyError:
            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                key = asdt
            except TypeError:
                pass

            key = Period(key, self.freq).ordinal
            return self._engine.get_loc(key)
Exemple #20
0
    def get_loc(self, key):
        """
        Get integer location for requested label

        Returns
        -------
        loc : int
        """
        try:
            return self._engine.get_loc(key)
        except KeyError:
            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                key = asdt
            except TypeError:
                pass

            key = Period(key, self.freq).ordinal
            return self._engine.get_loc(key)
Exemple #21
0
    def get_value(self, series, key):
        """
        Fast lookup of value from 1-dimensional ndarray. Only use this if you
        know what you're doing
        """
        s = com._values_from_object(series)
        try:
            return com._maybe_box(self,
                                  super(PeriodIndex, self).get_value(s, key),
                                  series, key)
        except (KeyError, IndexError):
            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                grp = frequencies.Resolution.get_freq_group(reso)
                freqn = frequencies.get_freq_group(self.freq)

                vals = self._values

                # if our data is higher resolution than requested key, slice
                if grp < freqn:
                    iv = Period(asdt, freq=(grp, 1))
                    ord1 = iv.asfreq(self.freq, how='S').ordinal
                    ord2 = iv.asfreq(self.freq, how='E').ordinal

                    if ord2 < vals[0] or ord1 > vals[-1]:
                        raise KeyError(key)

                    pos = np.searchsorted(self._values, [ord1, ord2])
                    key = slice(pos[0], pos[1] + 1)
                    return series[key]
                elif grp == freqn:
                    key = Period(asdt, freq=self.freq).ordinal
                    return com._maybe_box(self, self._engine.get_value(s, key),
                                          series, key)
                else:
                    raise KeyError(key)
            except TypeError:
                pass

            key = Period(key, self.freq).ordinal
            return com._maybe_box(self, self._engine.get_value(s, key), series,
                                  key)
Exemple #22
0
    def get_value(self, series, key):
        """
        Fast lookup of value from 1-dimensional ndarray. Only use this if you
        know what you're doing
        """
        s = com._values_from_object(series)
        try:
            return com._maybe_box(self,
                                  super(PeriodIndex, self).get_value(s, key),
                                  series, key)
        except (KeyError, IndexError):
            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                grp = frequencies.Resolution.get_freq_group(reso)
                freqn = frequencies.get_freq_group(self.freq)

                vals = self.values

                # if our data is higher resolution than requested key, slice
                if grp < freqn:
                    iv = Period(asdt, freq=(grp, 1))
                    ord1 = iv.asfreq(self.freq, how='S').ordinal
                    ord2 = iv.asfreq(self.freq, how='E').ordinal

                    if ord2 < vals[0] or ord1 > vals[-1]:
                        raise KeyError(key)

                    pos = np.searchsorted(self.values, [ord1, ord2])
                    key = slice(pos[0], pos[1] + 1)
                    return series[key]
                elif grp == freqn:
                    key = Period(asdt, freq=self.freq).ordinal
                    return com._maybe_box(self, self._engine.get_value(s, key),
                                          series, key)
                else:
                    raise KeyError(key)
            except TypeError:
                pass

            key = Period(key, self.freq).ordinal
            return com._maybe_box(self, self._engine.get_value(s, key),
                                  series, key)
Exemple #23
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError('Partial indexing only valid for '
                             'ordered time series')

        key, parsed, reso = parse_time_string(key, self.freq)

        grp = _freq_mod._infer_period_group(reso)
        freqn = _freq_mod._period_group(self.freq)

        if reso == 'year':
            t1 = Period(year=parsed.year, freq='A')
        elif reso == 'month':
            t1 = Period(year=parsed.year, month=parsed.month, freq='M')
        elif reso == 'quarter':
            q = (parsed.month - 1) // 3 + 1
            t1 = Period(year=parsed.year, quarter=q, freq='Q-DEC')
        elif reso == 'day' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        freq='D')
        elif reso == 'hour' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, freq='H')
        elif reso == 'minute' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, minute=parsed.minute, freq='T')
        elif reso == 'second' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, minute=parsed.minute, second=parsed.second,
                        freq='S')
        else:
            raise KeyError(key)

        ordinals = self.values

        t2 = t1.asfreq(self.freq, how='end')
        t1 = t1.asfreq(self.freq, how='start')

        left = ordinals.searchsorted(t1.ordinal, side='left')
        right = ordinals.searchsorted(t2.ordinal, side='right')
        return slice(left, right)
Exemple #24
0
    def _get_string_slice(self, key):
        if not self.is_monotonic:
            raise ValueError('Partial indexing only valid for '
                             'ordered time series')

        key, parsed, reso = parse_time_string(key, self.freq)

        grp = _freq_mod._infer_period_group(reso)
        freqn = _freq_mod._period_group(self.freq)

        if reso == 'year':
            t1 = Period(year=parsed.year, freq='A')
        elif reso == 'month':
            t1 = Period(year=parsed.year, month=parsed.month, freq='M')
        elif reso == 'quarter':
            q = (parsed.month - 1) // 3 + 1
            t1 = Period(year=parsed.year, quarter=q, freq='Q-DEC')
        elif reso == 'day' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        freq='D')
        elif reso == 'hour' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, freq='H')
        elif reso == 'minute' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, minute=parsed.minute, freq='T')
        elif reso == 'second' and grp < freqn:
            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
                        hour=parsed.hour, minute=parsed.minute, second=parsed.second,
                        freq='S')
        else:
            raise KeyError(key)

        ordinals = self.values

        t2 = t1.asfreq(self.freq, how='end')
        t1 = t1.asfreq(self.freq, how='start')

        left = ordinals.searchsorted(t1.ordinal, side='left')
        right = ordinals.searchsorted(t2.ordinal, side='right')
        return slice(left, right)
Exemple #25
0
    def test_parsers_dayfirst_yearfirst(self):
        # str : dayfirst, yearfirst, expected
        cases = {
            '10-11-12': [(False, False, datetime.datetime(2012, 10, 11)),
                         (True, False, datetime.datetime(2012, 11, 10)),
                         (False, True, datetime.datetime(2010, 11, 12)),
                         (True, True, datetime.datetime(2010, 11, 12))],
            '20/12/21': [(False, False, datetime.datetime(2021, 12, 20)),
                         (True, False, datetime.datetime(2021, 12, 20)),
                         (False, True, datetime.datetime(2020, 12, 21)),
                         (True, True, datetime.datetime(2020, 12, 21))]
        }

        tm._skip_if_no_dateutil()
        from dateutil.parser import parse
        for date_str, values in compat.iteritems(cases):
            for dayfirst, yearfirst, expected in values:
                result1, _, _ = tools.parse_time_string(date_str,
                                                        dayfirst=dayfirst,
                                                        yearfirst=yearfirst)

                result2 = to_datetime(date_str,
                                      dayfirst=dayfirst,
                                      yearfirst=yearfirst)

                result3 = DatetimeIndex([date_str],
                                        dayfirst=dayfirst,
                                        yearfirst=yearfirst)[0]

                # Timestamp doesn't support dayfirst and yearfirst

                self.assertEqual(result1, expected)
                self.assertEqual(result2, expected)
                self.assertEqual(result3, expected)

                # compare with dateutil result
                dateutil_result = parse(date_str,
                                        dayfirst=dayfirst,
                                        yearfirst=yearfirst)
                self.assertEqual(dateutil_result, expected)
Exemple #26
0
    def test_parsers_timestring(self):
        tm._skip_if_no_dateutil()
        from dateutil.parser import parse

        # must be the same as dateutil result
        cases = {'10:15': (parse('10:15'), datetime.datetime(1, 1, 1, 10, 15)),
                 '9:05': (parse('9:05'), datetime.datetime(1, 1, 1, 9, 5)) }

        for date_str, (exp_now, exp_def) in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str)
            result2 = to_datetime(date_str)
            result3 = to_datetime([date_str])
            result4 = Timestamp(date_str)
            result5 = DatetimeIndex([date_str])[0]
            # parse time string return time string based on default date
            # others are not, and can't be changed because it is used in
            # time series plot
            self.assertEqual(result1, exp_def)
            self.assertEqual(result2, exp_now)
            self.assertEqual(result3, exp_now)
            self.assertEqual(result4, exp_now)
            self.assertEqual(result5, exp_now)
Exemple #27
0
    def test_parsers_timestring(self):
        tm._skip_if_no_dateutil()
        from dateutil.parser import parse

        # must be the same as dateutil result
        cases = {'10:15': (parse('10:15'), datetime.datetime(1, 1, 1, 10, 15)),
                 '9:05': (parse('9:05'), datetime.datetime(1, 1, 1, 9, 5))}

        for date_str, (exp_now, exp_def) in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str)
            result2 = to_datetime(date_str)
            result3 = to_datetime([date_str])
            result4 = Timestamp(date_str)
            result5 = DatetimeIndex([date_str])[0]
            # parse time string return time string based on default date
            # others are not, and can't be changed because it is used in
            # time series plot
            self.assertEqual(result1, exp_def)
            self.assertEqual(result2, exp_now)
            self.assertEqual(result3, exp_now)
            self.assertEqual(result4, exp_now)
            self.assertEqual(result5, exp_now)
Exemple #28
0
def _get_date_and_freq(value, freq):
    value = value.upper()
    dt, _, reso = parse_time_string(value, freq)

    if freq is None:
        if reso == 'year':
            freq = 'A'
        elif reso == 'quarter':
            freq = 'Q'
        elif reso == 'month':
            freq = 'M'
        elif reso == 'day':
            freq = 'D'
        elif reso == 'hour':
            freq = 'H'
        elif reso == 'minute':
            freq = 'T'
        elif reso == 'second':
            freq = 'S'
        else:
            raise ValueError("Could not infer frequency for period")

    return dt, freq
Exemple #29
0
def _get_date_and_freq(value, freq):
    value = value.upper()
    dt, _, reso = parse_time_string(value, freq)

    if freq is None:
        if reso == 'year':
            freq = 'A'
        elif reso == 'quarter':
            freq = 'Q'
        elif reso == 'month':
            freq = 'M'
        elif reso == 'day':
            freq = 'D'
        elif reso == 'hour':
            freq = 'H'
        elif reso == 'minute':
            freq = 'T'
        elif reso == 'second':
            freq = 'S'
        else:
            raise ValueError("Invalid frequency or could not infer: %s" % reso)

    return dt, freq
Exemple #30
0
    def get_value(self, series, key):
        """
        Fast lookup of value from 1-dimensional ndarray. Only use this if you
        know what you're doing
        """
        try:
            return super(PeriodIndex, self).get_value(series, key)
        except (KeyError, IndexError):
            try:
                asdt, parsed, reso = parse_time_string(key, self.freq)
                grp = _freq_mod._infer_period_group(reso)
                freqn = _freq_mod._period_group(self.freq)

                vals = self.values

                # if our data is higher resolution than requested key, slice
                if grp < freqn:
                    iv = Period(asdt, freq=(grp, 1))
                    ord1 = iv.asfreq(self.freq, how="S").ordinal
                    ord2 = iv.asfreq(self.freq, how="E").ordinal

                    if ord2 < vals[0] or ord1 > vals[-1]:
                        raise KeyError(key)

                    pos = np.searchsorted(self.values, [ord1, ord2])
                    key = slice(pos[0], pos[1] + 1)
                    return series[key]
                else:
                    key = Period(asdt, freq=self.freq)
                    return self._engine.get_value(series, key.ordinal)
            except TypeError:
                pass
            except KeyError:
                pass

            key = Period(key, self.freq)
            return self._engine.get_value(series, key.ordinal)
Exemple #31
0
 def _get_string_slice(self, key):
     asdt, parsed, reso = parse_time_string(key)
     key = asdt
     loc = self._partial_date_slice(reso, parsed)
     return loc
Exemple #32
0
    def __init__(self, value=None, freq=None, ordinal=None,
                 year=None, month=1, quarter=None, day=1,
                 hour=0, minute=0, second=0):
        # freq points to a tuple (base, mult);  base is one of the defined
        # periods such as A, Q, etc. Every five minutes would be, e.g.,
        # ('T', 5) but may be passed in as a string like '5T'

        self.freq = None

        # ordinal is the period offset from the gregorian proleptic epoch
        self.ordinal = None

        if ordinal is not None and value is not None:
            raise ValueError(("Only value or ordinal but not both should be "
                              "given but not both"))
        elif ordinal is not None:
            if not com.is_integer(ordinal):
                raise ValueError("Ordinal must be an integer")
            if freq is None:
                raise ValueError('Must supply freq for ordinal value')
            self.ordinal = ordinal

        elif value is None:
            if freq is None:
                raise ValueError("If value is None, freq cannot be None")

            self.ordinal = _ordinal_from_fields(year, month, quarter, day,
                                                hour, minute, second, freq)

        elif isinstance(value, Period):
            other = value
            if freq is None or _gfc(freq) == _gfc(other.freq):
                self.ordinal = other.ordinal
                freq = other.freq
            else:
                converted = other.asfreq(freq)
                self.ordinal = converted.ordinal

        elif com._is_null_datelike_scalar(value) or value in tslib._nat_strings:
            self.ordinal = tslib.iNaT
            if freq is None:
                raise ValueError("If value is NaT, freq cannot be None "
                                 "because it cannot be inferred")

        elif isinstance(value, compat.string_types) or com.is_integer(value):
            if com.is_integer(value):
                value = str(value)
            value = value.upper()

            dt, _, reso = parse_time_string(value, freq)
            if freq is None:
                try:
                    freq = _freq_mod.Resolution.get_freq(reso)
                except KeyError:
                    raise ValueError("Invalid frequency or could not infer: %s" % reso)

        elif isinstance(value, datetime):
            dt = value
            if freq is None:
                raise ValueError('Must supply freq for datetime value')
        elif isinstance(value, date):
            dt = datetime(year=value.year, month=value.month, day=value.day)
            if freq is None:
                raise ValueError('Must supply freq for datetime value')
        else:
            msg = "Value must be Period, string, integer, or datetime"
            raise ValueError(msg)

        base, mult = _gfc(freq)
        if mult != 1:
            # TODO: Better error message - this is slightly confusing
            raise ValueError('Only mult == 1 supported')

        if self.ordinal is None:
            self.ordinal = tslib.period_ordinal(dt.year, dt.month, dt.day,
                                                dt.hour, dt.minute, dt.second, dt.microsecond, 0,
                                                base)

        self.freq = _freq_mod._get_freq_str(base)
Exemple #33
0
    def __init__(self,
                 value=None,
                 freq=None,
                 ordinal=None,
                 year=None,
                 month=1,
                 quarter=None,
                 day=1,
                 hour=0,
                 minute=0,
                 second=0):
        # freq points to a tuple (base, mult);  base is one of the defined
        # periods such as A, Q, etc. Every five minutes would be, e.g.,
        # ('T', 5) but may be passed in as a string like '5T'

        self.freq = None

        # ordinal is the period offset from the gregorian proleptic epoch
        self.ordinal = None

        if ordinal is not None and value is not None:
            raise ValueError(("Only value or ordinal but not both should be "
                              "given but not both"))
        elif ordinal is not None:
            if not com.is_integer(ordinal):
                raise ValueError("Ordinal must be an integer")
            if freq is None:
                raise ValueError('Must supply freq for ordinal value')
            self.ordinal = ordinal

        elif value is None:
            if freq is None:
                raise ValueError("If value is None, freq cannot be None")

            self.ordinal = _ordinal_from_fields(year, month, quarter, day,
                                                hour, minute, second, freq)

        elif isinstance(value, Period):
            other = value
            if freq is None or _gfc(freq) == _gfc(other.freq):
                self.ordinal = other.ordinal
                freq = other.freq
            else:
                converted = other.asfreq(freq)
                self.ordinal = converted.ordinal

        elif com._is_null_datelike_scalar(
                value) or value in tslib._nat_strings:
            self.ordinal = tslib.iNaT
            if freq is None:
                raise ValueError("If value is NaT, freq cannot be None "
                                 "because it cannot be inferred")

        elif isinstance(value, compat.string_types) or com.is_integer(value):
            if com.is_integer(value):
                value = str(value)
            value = value.upper()

            dt, _, reso = parse_time_string(value, freq)
            if freq is None:
                try:
                    freq = _freq_mod.Resolution.get_freq(reso)
                except KeyError:
                    raise ValueError(
                        "Invalid frequency or could not infer: %s" % reso)

        elif isinstance(value, datetime):
            dt = value
            if freq is None:
                raise ValueError('Must supply freq for datetime value')
        elif isinstance(value, date):
            dt = datetime(year=value.year, month=value.month, day=value.day)
            if freq is None:
                raise ValueError('Must supply freq for datetime value')
        else:
            msg = "Value must be Period, string, integer, or datetime"
            raise ValueError(msg)

        base, mult = _gfc(freq)
        if mult != 1:
            # TODO: Better error message - this is slightly confusing
            raise ValueError('Only mult == 1 supported')

        if self.ordinal is None:
            self.ordinal = tslib.period_ordinal(dt.year, dt.month, dt.day,
                                                dt.hour, dt.minute, dt.second,
                                                dt.microsecond, 0, base)

        self.freq = _freq_mod._get_freq_str(base)
Exemple #34
0
 def parser(x):
     result = parse_time_string(x, offset)
     return result[0]
Exemple #35
0
    def __init__(self, value=None, freq=None,
                 year=None, month=1, quarter=None, day=1,
                 hour=0, minute=0, second=0):
        """
        Represents an period of time

        Parameters
        ----------
        value : Period or basestring, default None
            The time period represented (e.g., '4Q2005')
        freq : str, default None
            e.g., 'B' for businessday, ('T', 5) or '5T' for 5 minutes
        year : int, default None
        month : int, default 1
        quarter : int, default None
        day : int, default 1
        hour : int, default 0
        minute : int, default 0
        second : int, default 0
        """
        # freq points to a tuple (base, mult);  base is one of the defined
        # periods such as A, Q, etc. Every five minutes would be, e.g.,
        # ('T', 5) but may be passed in as a string like '5T'

        self.freq = None

        # ordinal is the period offset from the gregorian proleptic epoch

        self.ordinal = None

        if value is None:
            if freq is None:
                raise ValueError("If value is None, freq cannot be None")

            if year is None:
                raise ValueError("If value is None, year cannot be None")

            if quarter is not None:
                month = (quarter - 1) * 3 + 1

            base, mult = _gfc(freq)

            self.ordinal = lib.period_ordinal(year, month, day, hour, minute,
                                            second, base, mult)

        elif isinstance(value, Period):
            other = value
            if freq is None or _gfc(freq) == _gfc(other.freq):
                self.ordinal = other.ordinal
                freq = other.freq
            else:
                converted = other.asfreq(freq)
                self.ordinal = converted.ordinal

        elif isinstance(value, basestring):
            value = value.upper()
            dt, parsed, reso = parse_time_string(value)

            if freq is None:
                if reso == 'year':
                    freq = 'A'
                elif reso == 'quarter':
                    freq = 'Q'
                elif reso == 'month':
                    freq = 'M'
                elif reso == 'day':
                    freq = 'D'
                elif reso == 'hour':
                    freq = 'H'
                elif reso == 'minute':
                    freq = 'T'
                elif reso == 'second':
                    freq = 'S'
                else:
                    raise ValueError("Could not infer frequency for period")

        elif isinstance(value, datetime):
            dt = value
            if freq is None:
                raise ValueError('Must supply freq for datetime value')
        elif com.is_integer(value):
            if value <= 0:
                raise ValueError("Value must be positive")
            self.ordinal = value
            if freq is None:
                raise ValueError('Must supply freq for ordinal value')
        else:
            msg = "Value must be Period, string, integer, or datetime"
            raise ValueError(msg)

        base, mult = _gfc(freq)

        if self.ordinal is None:
            self.ordinal = lib.period_ordinal(dt.year, dt.month, dt.day, dt.hour,
                                            dt.minute, dt.second, base, mult)

        self.freq = _freq_mod._get_freq_str(base, mult)
Exemple #36
0
 def parser(x):
     result = parse_time_string(x, offset)
     return result[0]
Exemple #37
0
    def test_parsers(self):

        # https://github.com/dateutil/dateutil/issues/217
        import dateutil
        yearfirst = dateutil.__version__ >= LooseVersion('2.5.0')

        cases = {'2011-01-01': datetime.datetime(2011, 1, 1),
                 '2Q2005': datetime.datetime(2005, 4, 1),
                 '2Q05': datetime.datetime(2005, 4, 1),
                 '2005Q1': datetime.datetime(2005, 1, 1),
                 '05Q1': datetime.datetime(2005, 1, 1),
                 '2011Q3': datetime.datetime(2011, 7, 1),
                 '11Q3': datetime.datetime(2011, 7, 1),
                 '3Q2011': datetime.datetime(2011, 7, 1),
                 '3Q11': datetime.datetime(2011, 7, 1),

                 # quarterly without space
                 '2000Q4': datetime.datetime(2000, 10, 1),
                 '00Q4': datetime.datetime(2000, 10, 1),
                 '4Q2000': datetime.datetime(2000, 10, 1),
                 '4Q00': datetime.datetime(2000, 10, 1),
                 '2000q4': datetime.datetime(2000, 10, 1),
                 '2000-Q4': datetime.datetime(2000, 10, 1),
                 '00-Q4': datetime.datetime(2000, 10, 1),
                 '4Q-2000': datetime.datetime(2000, 10, 1),
                 '4Q-00': datetime.datetime(2000, 10, 1),
                 '00q4': datetime.datetime(2000, 10, 1),
                 '2005': datetime.datetime(2005, 1, 1),
                 '2005-11': datetime.datetime(2005, 11, 1),
                 '2005 11': datetime.datetime(2005, 11, 1),
                 '11-2005': datetime.datetime(2005, 11, 1),
                 '11 2005': datetime.datetime(2005, 11, 1),
                 '200511': datetime.datetime(2020, 5, 11),
                 '20051109': datetime.datetime(2005, 11, 9),
                 '20051109 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '20051109 08H': datetime.datetime(2005, 11, 9, 8, 0),
                 '2005-11-09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '2005-11-09 08H': datetime.datetime(2005, 11, 9, 8, 0),
                 '2005/11/09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '2005/11/09 08H': datetime.datetime(2005, 11, 9, 8, 0),
                 "Thu Sep 25 10:36:28 2003": datetime.datetime(2003, 9, 25, 10,
                                                               36, 28),
                 "Thu Sep 25 2003": datetime.datetime(2003, 9, 25),
                 "Sep 25 2003": datetime.datetime(2003, 9, 25),
                 "January 1 2014": datetime.datetime(2014, 1, 1),

                 # GH 10537
                 '2014-06': datetime.datetime(2014, 6, 1),
                 '06-2014': datetime.datetime(2014, 6, 1),
                 '2014-6': datetime.datetime(2014, 6, 1),
                 '6-2014': datetime.datetime(2014, 6, 1),

                 '20010101 12': datetime.datetime(2001, 1, 1, 12),
                 '20010101 1234': datetime.datetime(2001, 1, 1, 12, 34),
                 '20010101 123456': datetime.datetime(2001, 1, 1, 12, 34, 56),
                 }

        for date_str, expected in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str,
                                                    yearfirst=yearfirst)
            result2 = to_datetime(date_str, yearfirst=yearfirst)
            result3 = to_datetime([date_str], yearfirst=yearfirst)
            # result5 is used below
            result4 = to_datetime(np.array([date_str], dtype=object),
                                  yearfirst=yearfirst)
            result6 = DatetimeIndex([date_str], yearfirst=yearfirst)
            # result7 is used below
            result8 = DatetimeIndex(Index([date_str]), yearfirst=yearfirst)
            result9 = DatetimeIndex(Series([date_str]), yearfirst=yearfirst)

            for res in [result1, result2]:
                self.assertEqual(res, expected)
            for res in [result3, result4, result6, result8, result9]:
                exp = DatetimeIndex([pd.Timestamp(expected)])
                tm.assert_index_equal(res, exp)

            # these really need to have yearfist, but we don't support
            if not yearfirst:
                result5 = Timestamp(date_str)
                self.assertEqual(result5, expected)
                result7 = date_range(date_str, freq='S', periods=1,
                                     yearfirst=yearfirst)
                self.assertEqual(result7, expected)

        # NaT
        result1, _, _ = tools.parse_time_string('NaT')
        result2 = to_datetime('NaT')
        result3 = Timestamp('NaT')
        result4 = DatetimeIndex(['NaT'])[0]
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
Exemple #38
0
    def test_parsers(self):
        cases = {
            '2011-01-01': datetime.datetime(2011, 1, 1),
            '2Q2005': datetime.datetime(2005, 4, 1),
            '2Q05': datetime.datetime(2005, 4, 1),
            '2005Q1': datetime.datetime(2005, 1, 1),
            '05Q1': datetime.datetime(2005, 1, 1),
            '2011Q3': datetime.datetime(2011, 7, 1),
            '11Q3': datetime.datetime(2011, 7, 1),
            '3Q2011': datetime.datetime(2011, 7, 1),
            '3Q11': datetime.datetime(2011, 7, 1),

            # quarterly without space
            '2000Q4': datetime.datetime(2000, 10, 1),
            '00Q4': datetime.datetime(2000, 10, 1),
            '4Q2000': datetime.datetime(2000, 10, 1),
            '4Q00': datetime.datetime(2000, 10, 1),
            '2000q4': datetime.datetime(2000, 10, 1),
            '2000-Q4': datetime.datetime(2000, 10, 1),
            '00-Q4': datetime.datetime(2000, 10, 1),
            '4Q-2000': datetime.datetime(2000, 10, 1),
            '4Q-00': datetime.datetime(2000, 10, 1),
            '2000q4': datetime.datetime(2000, 10, 1),
            '00q4': datetime.datetime(2000, 10, 1),
            '2005': datetime.datetime(2005, 1, 1),
            '2005-11': datetime.datetime(2005, 11, 1),
            '2005 11': datetime.datetime(2005, 11, 1),
            '11-2005': datetime.datetime(2005, 11, 1),
            '11 2005': datetime.datetime(2005, 11, 1),
            '200511': datetime.datetime(2020, 5, 11),
            '20051109': datetime.datetime(2005, 11, 9),
            '20051109 10:15': datetime.datetime(2005, 11, 9, 10, 15),
            '20051109 08H': datetime.datetime(2005, 11, 9, 8, 0),
            '2005-11-09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
            '2005-11-09 08H': datetime.datetime(2005, 11, 9, 8, 0),
            '2005/11/09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
            '2005/11/09 08H': datetime.datetime(2005, 11, 9, 8, 0),
            "Thu Sep 25 10:36:28 2003":
            datetime.datetime(2003, 9, 25, 10, 36, 28),
            "Thu Sep 25 2003": datetime.datetime(2003, 9, 25),
            "Sep 25 2003": datetime.datetime(2003, 9, 25),
            "January 1 2014": datetime.datetime(2014, 1, 1),

            # GH 10537
            '2014-06': datetime.datetime(2014, 6, 1),
            '06-2014': datetime.datetime(2014, 6, 1),
            '2014-6': datetime.datetime(2014, 6, 1),
            '6-2014': datetime.datetime(2014, 6, 1),
        }

        for date_str, expected in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str)
            result2 = to_datetime(date_str)
            result3 = to_datetime([date_str])
            result4 = to_datetime(np.array([date_str], dtype=object))
            result5 = Timestamp(date_str)
            result6 = DatetimeIndex([date_str])[0]
            result7 = date_range(date_str, freq='S', periods=1)
            self.assertEqual(result1, expected)
            self.assertEqual(result2, expected)
            self.assertEqual(result3, expected)
            self.assertEqual(result4, expected)
            self.assertEqual(result5, expected)
            self.assertEqual(result6, expected)
            self.assertEqual(result7, expected)

        # NaT
        result1, _, _ = tools.parse_time_string('NaT')
        result2 = to_datetime('NaT')
        result3 = Timestamp('NaT')
        result4 = DatetimeIndex(['NaT'])[0]
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
Exemple #39
0
 def parser(x):
     result = parse_time_string(x,
                                offset,
                                dayfirst=dayfirst,
                                yearfirst=None)
     return result[0]
Exemple #40
0
 def parser(x):
     result = parse_time_string(x, offset, dayfirst=dayfirst,
                                yearfirst=yearfirst)
     return result[0]
Exemple #41
0
    def test_parsers(self):
        cases = {
            "2011-01-01": datetime.datetime(2011, 1, 1),
            "2Q2005": datetime.datetime(2005, 4, 1),
            "2Q05": datetime.datetime(2005, 4, 1),
            "2005Q1": datetime.datetime(2005, 1, 1),
            "05Q1": datetime.datetime(2005, 1, 1),
            "2011Q3": datetime.datetime(2011, 7, 1),
            "11Q3": datetime.datetime(2011, 7, 1),
            "3Q2011": datetime.datetime(2011, 7, 1),
            "3Q11": datetime.datetime(2011, 7, 1),
            # quarterly without space
            "2000Q4": datetime.datetime(2000, 10, 1),
            "00Q4": datetime.datetime(2000, 10, 1),
            "4Q2000": datetime.datetime(2000, 10, 1),
            "4Q00": datetime.datetime(2000, 10, 1),
            "2000q4": datetime.datetime(2000, 10, 1),
            "2000-Q4": datetime.datetime(2000, 10, 1),
            "00-Q4": datetime.datetime(2000, 10, 1),
            "4Q-2000": datetime.datetime(2000, 10, 1),
            "4Q-00": datetime.datetime(2000, 10, 1),
            "2000q4": datetime.datetime(2000, 10, 1),
            "00q4": datetime.datetime(2000, 10, 1),
            "2005": datetime.datetime(2005, 1, 1),
            "2005-11": datetime.datetime(2005, 11, 1),
            "2005 11": datetime.datetime(2005, 11, 1),
            "11-2005": datetime.datetime(2005, 11, 1),
            "11 2005": datetime.datetime(2005, 11, 1),
            "200511": datetime.datetime(2020, 5, 11),
            "20051109": datetime.datetime(2005, 11, 9),
            "20051109 10:15": datetime.datetime(2005, 11, 9, 10, 15),
            "20051109 08H": datetime.datetime(2005, 11, 9, 8, 0),
            "2005-11-09 10:15": datetime.datetime(2005, 11, 9, 10, 15),
            "2005-11-09 08H": datetime.datetime(2005, 11, 9, 8, 0),
            "2005/11/09 10:15": datetime.datetime(2005, 11, 9, 10, 15),
            "2005/11/09 08H": datetime.datetime(2005, 11, 9, 8, 0),
            "Thu Sep 25 10:36:28 2003": datetime.datetime(2003, 9, 25, 10, 36, 28),
            "Thu Sep 25 2003": datetime.datetime(2003, 9, 25),
            "Sep 25 2003": datetime.datetime(2003, 9, 25),
            "January 1 2014": datetime.datetime(2014, 1, 1),
            # GH 10537
            "2014-06": datetime.datetime(2014, 6, 1),
            "06-2014": datetime.datetime(2014, 6, 1),
            "2014-6": datetime.datetime(2014, 6, 1),
            "6-2014": datetime.datetime(2014, 6, 1),
        }

        for date_str, expected in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str)
            result2 = to_datetime(date_str)
            result3 = to_datetime([date_str])
            result4 = to_datetime(np.array([date_str], dtype=object))
            result5 = Timestamp(date_str)
            result6 = DatetimeIndex([date_str])[0]
            result7 = date_range(date_str, freq="S", periods=1)
            self.assertEqual(result1, expected)
            self.assertEqual(result2, expected)
            self.assertEqual(result3, expected)
            self.assertEqual(result4, expected)
            self.assertEqual(result5, expected)
            self.assertEqual(result6, expected)
            self.assertEqual(result7, expected)

        # NaT
        result1, _, _ = tools.parse_time_string("NaT")
        result2 = to_datetime("NaT")
        result3 = Timestamp("NaT")
        result4 = DatetimeIndex(["NaT"])[0]
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
Exemple #42
0
    def test_parsers(self):
        cases = {'2011-01-01': datetime.datetime(2011, 1, 1),
                 '2Q2005': datetime.datetime(2005, 4, 1),
                 '2Q05': datetime.datetime(2005, 4, 1),
                 '2005Q1': datetime.datetime(2005, 1, 1),
                 '05Q1': datetime.datetime(2005, 1, 1),
                 '2011Q3': datetime.datetime(2011, 7, 1),
                 '11Q3': datetime.datetime(2011, 7, 1),
                 '3Q2011': datetime.datetime(2011, 7, 1),
                 '3Q11': datetime.datetime(2011, 7, 1),

                 # quarterly without space
                 '2000Q4': datetime.datetime(2000, 10, 1),
                 '00Q4': datetime.datetime(2000, 10, 1),
                 '4Q2000': datetime.datetime(2000, 10, 1),
                 '4Q00': datetime.datetime(2000, 10, 1),
                 '2000q4': datetime.datetime(2000, 10, 1),

                 '2000-Q4': datetime.datetime(2000, 10, 1),
                 '00-Q4': datetime.datetime(2000, 10, 1),
                 '4Q-2000': datetime.datetime(2000, 10, 1),
                 '4Q-00': datetime.datetime(2000, 10, 1),

                 '2000q4': datetime.datetime(2000, 10, 1),
                 '00q4': datetime.datetime(2000, 10, 1),

                 '2005': datetime.datetime(2005, 1, 1),
                 '2005-11': datetime.datetime(2005, 11, 1),
                 '2005 11': datetime.datetime(2005, 11, 1),
                 '11-2005': datetime.datetime(2005, 11, 1),
                 '11 2005': datetime.datetime(2005, 11, 1),
                 '200511': datetime.datetime(2020, 5, 11),
                 '20051109': datetime.datetime(2005, 11, 9),

                 '20051109 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '20051109 08H': datetime.datetime(2005, 11, 9, 8, 0),

                 '2005-11-09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '2005-11-09 08H': datetime.datetime(2005, 11, 9, 8, 0),
                 '2005/11/09 10:15': datetime.datetime(2005, 11, 9, 10, 15),
                 '2005/11/09 08H': datetime.datetime(2005, 11, 9, 8, 0),

                 "Thu Sep 25 10:36:28 2003": datetime.datetime(2003, 9, 25, 10, 36, 28),
                 "Thu Sep 25 2003": datetime.datetime(2003, 9, 25),
                 "Sep 25 2003": datetime.datetime(2003, 9, 25),
                 "January 1 2014": datetime.datetime(2014, 1, 1),

                 # GH 10537
                 '2014-06': datetime.datetime(2014, 6, 1),
                 '06-2014': datetime.datetime(2014, 6, 1),
                 '2014-6': datetime.datetime(2014, 6, 1),
                 '6-2014': datetime.datetime(2014, 6, 1),
                 }

        for date_str, expected in compat.iteritems(cases):
            result1, _, _ = tools.parse_time_string(date_str)
            result2 = to_datetime(date_str)
            result3 = to_datetime([date_str])
            result4 = to_datetime(np.array([date_str], dtype=object))
            result5 = Timestamp(date_str)
            result6 = DatetimeIndex([date_str])[0]
            result7 = date_range(date_str, freq='S', periods=1)
            self.assertEqual(result1, expected)
            self.assertEqual(result2, expected)
            self.assertEqual(result3, expected)
            self.assertEqual(result4, expected)
            self.assertEqual(result5, expected)
            self.assertEqual(result6, expected)
            self.assertEqual(result7, expected)

        # NaT
        result1, _, _ = tools.parse_time_string('NaT')
        result2 = to_datetime('NaT')
        result3 = Timestamp('NaT')
        result4 = DatetimeIndex(['NaT'])[0]
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
        self.assertTrue(result1 is tslib.NaT)
Exemple #43
0
    def test_parsers_dayfirst_yearfirst(self):
        tm._skip_if_no_dateutil()

        # OK
        # 2.5.1 10-11-12   [dayfirst=0, yearfirst=0] -> 2012-10-11 00:00:00
        # 2.5.2 10-11-12   [dayfirst=0, yearfirst=1] -> 2012-10-11 00:00:00
        # 2.5.3 10-11-12   [dayfirst=0, yearfirst=0] -> 2012-10-11 00:00:00

        # OK
        # 2.5.1 10-11-12   [dayfirst=0, yearfirst=1] -> 2010-11-12 00:00:00
        # 2.5.2 10-11-12   [dayfirst=0, yearfirst=1] -> 2010-11-12 00:00:00
        # 2.5.3 10-11-12   [dayfirst=0, yearfirst=1] -> 2010-11-12 00:00:00

        # bug fix in 2.5.2
        # 2.5.1 10-11-12   [dayfirst=1, yearfirst=1] -> 2010-11-12 00:00:00
        # 2.5.2 10-11-12   [dayfirst=1, yearfirst=1] -> 2010-12-11 00:00:00
        # 2.5.3 10-11-12   [dayfirst=1, yearfirst=1] -> 2010-12-11 00:00:00

        # OK
        # 2.5.1 10-11-12   [dayfirst=1, yearfirst=0] -> 2012-11-10 00:00:00
        # 2.5.2 10-11-12   [dayfirst=1, yearfirst=0] -> 2012-11-10 00:00:00
        # 2.5.3 10-11-12   [dayfirst=1, yearfirst=0] -> 2012-11-10 00:00:00

        # OK
        # 2.5.1 20/12/21   [dayfirst=0, yearfirst=0] -> 2021-12-20 00:00:00
        # 2.5.2 20/12/21   [dayfirst=0, yearfirst=0] -> 2021-12-20 00:00:00
        # 2.5.3 20/12/21   [dayfirst=0, yearfirst=0] -> 2021-12-20 00:00:00

        # OK
        # 2.5.1 20/12/21   [dayfirst=0, yearfirst=1] -> 2020-12-21 00:00:00
        # 2.5.2 20/12/21   [dayfirst=0, yearfirst=1] -> 2020-12-21 00:00:00
        # 2.5.3 20/12/21   [dayfirst=0, yearfirst=1] -> 2020-12-21 00:00:00

        # revert of bug in 2.5.2
        # 2.5.1 20/12/21   [dayfirst=1, yearfirst=1] -> 2020-12-21 00:00:00
        # 2.5.2 20/12/21   [dayfirst=1, yearfirst=1] -> month must be in 1..12
        # 2.5.3 20/12/21   [dayfirst=1, yearfirst=1] -> 2020-12-21 00:00:00

        # OK
        # 2.5.1 20/12/21   [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00
        # 2.5.2 20/12/21   [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00
        # 2.5.3 20/12/21   [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00

        import dateutil
        is_lt_253 = dateutil.__version__ < LooseVersion('2.5.3')

        # str : dayfirst, yearfirst, expected
        cases = {'10-11-12': [(False, False,
                               datetime.datetime(2012, 10, 11)),
                              (True, False,
                               datetime.datetime(2012, 11, 10)),
                              (False, True,
                               datetime.datetime(2010, 11, 12)),
                              (True, True,
                               datetime.datetime(2010, 12, 11))],
                 '20/12/21': [(False, False,
                               datetime.datetime(2021, 12, 20)),
                              (True, False,
                               datetime.datetime(2021, 12, 20)),
                              (False, True,
                               datetime.datetime(2020, 12, 21)),
                              (True, True,
                               datetime.datetime(2020, 12, 21))]}

        from dateutil.parser import parse
        for date_str, values in compat.iteritems(cases):
            for dayfirst, yearfirst, expected in values:

                # odd comparisons across version
                # let's just skip
                if dayfirst and yearfirst and is_lt_253:
                    continue

                # compare with dateutil result
                dateutil_result = parse(date_str, dayfirst=dayfirst,
                                        yearfirst=yearfirst)
                self.assertEqual(dateutil_result, expected)

                result1, _, _ = tools.parse_time_string(date_str,
                                                        dayfirst=dayfirst,
                                                        yearfirst=yearfirst)

                # we don't support dayfirst/yearfirst here:
                if not dayfirst and not yearfirst:
                    result2 = Timestamp(date_str)
                    self.assertEqual(result2, expected)

                result3 = to_datetime(date_str, dayfirst=dayfirst,
                                      yearfirst=yearfirst)

                result4 = DatetimeIndex([date_str], dayfirst=dayfirst,
                                        yearfirst=yearfirst)[0]

                self.assertEqual(result1, expected)
                self.assertEqual(result3, expected)
                self.assertEqual(result4, expected)
Exemple #44
0
 def _get_string_slice(self, key):
     freq = getattr(self, 'freqstr', getattr(self, 'inferred_freq', None))
     asdt, parsed, reso = parse_time_string(key, freq)
     key = asdt
     loc = self._partial_date_slice(reso, parsed)
     return loc