Пример #1
0
    def _index_to_records(self, df):
        metadata = {}
        index = df.index

        if isinstance(index, MultiIndex):
            # array of tuples to numpy cols. copy copy copy
            if len(df) > 0:
                ix_vals = list(map(np.array, [index.get_level_values(i) for i in range(index.nlevels)]))
            else:
                # empty multi index has no size, create empty arrays for recarry..
                ix_vals = [np.array([]) for n in index.names]
        else:
            ix_vals = [index.values]

        count = 0
        index_names = list(index.names)
        if isinstance(index, MultiIndex):
            for i, n in enumerate(index_names):
                if n is None:
                    index_names[i] = 'level_%d' % count
                    count += 1
                    log.info("Level in MultiIndex has no name, defaulting to %s" % index_names[i])
        elif index_names[0] is None:
            index_names = ['index']
            log.info("Index has no name, defaulting to 'index'")

        metadata['index'] = index_names

        if isinstance(index, DatetimeIndex) and index.tz is not None:
            metadata['index_tz'] = get_timezone(index.tz)
        elif isinstance(index, MultiIndex):
            metadata['index_tz'] = [get_timezone(i.tz) if isinstance(i, DatetimeIndex) else None for i in index.levels]

        return index_names, ix_vals, metadata
Пример #2
0
 def _infer(a, b):
     tz = a.tzinfo
     if b and b.tzinfo:
         if not (get_timezone(tz) == get_timezone(b.tzinfo)):
             raise AssertionError('Inputs must both have the same timezone,'
                                  ' {timezone1} != {timezone2}'
                                  .format(timezone1=tz, timezone2=b.tzinfo))
     return tz
Пример #3
0
 def _infer(a, b):
     tz = a.tzinfo
     if b and b.tzinfo:
         if not (get_timezone(tz) == get_timezone(b.tzinfo)):
             raise AssertionError('Inputs must both have the same timezone,'
                                  ' {timezone1} != {timezone2}'
                                  .format(timezone1=tz, timezone2=b.tzinfo))
     return tz
Пример #4
0
    def _has_same_tz(self, other):
        zzone = self._timezone

        # vzone sholdn't be None if value is non-datetime like
        if isinstance(other, np.datetime64):
            # convert to Timestamp as np.datetime64 doesn't have tz attr
            other = Timestamp(other)
        vzone = timezones.get_timezone(getattr(other, 'tzinfo', '__no_tz__'))
        return zzone == vzone
Пример #5
0
    def _has_same_tz(self, other):
        zzone = self._timezone

        # vzone sholdn't be None if value is non-datetime like
        if isinstance(other, np.datetime64):
            # convert to Timestamp as np.datetime64 doesn't have tz attr
            other = Timestamp(other)
        vzone = timezones.get_timezone(getattr(other, 'tzinfo', '__no_tz__'))
        return zzone == vzone
Пример #6
0
    def _index_to_records(self, df):
        metadata = {}
        index = df.index

        if isinstance(index, MultiIndex):
            # array of tuples to numpy cols. copy copy copy
            if len(df) > 0:
                ix_vals = list(
                    map(np.array, [
                        index.get_level_values(i) for i in range(index.nlevels)
                    ]))
            else:
                # empty multi index has no size, create empty arrays for recarry..
                ix_vals = [np.array([]) for n in index.names]
        else:
            ix_vals = [index.values]

        count = 0
        index_names = list(index.names)
        if isinstance(index, MultiIndex):
            for i, n in enumerate(index_names):
                if n is None:
                    index_names[i] = 'level_%d' % count
                    count += 1
                    log.info(
                        "Level in MultiIndex has no name, defaulting to %s" %
                        index_names[i])
        elif index_names[0] is None:
            index_names = ['index']
            log.info("Index has no name, defaulting to 'index'")

        metadata['index'] = index_names

        if isinstance(index, DatetimeIndex) and index.tz is not None:
            metadata['index_tz'] = get_timezone(index.tz)
        elif isinstance(index, MultiIndex):
            metadata['index_tz'] = [
                get_timezone(i.tz) if isinstance(i, DatetimeIndex) else None
                for i in index.levels
            ]

        return index_names, ix_vals, metadata
Пример #7
0
def _multi_index_to_records(index, empty_index):
    # array of tuples to numpy cols. copy copy copy
    if not empty_index:
        ix_vals = list(map(np.array, [index.get_level_values(i) for i in range(index.nlevels)]))
    else:
        # empty multi index has no size, create empty arrays for recarry..
        ix_vals = [np.array([]) for n in index.names]
    index_names = list(index.names)
    count = 0
    for i, n in enumerate(index_names):
        if n is None:
            index_names[i] = 'level_%d' % count
            count += 1
            log.info("Level in MultiIndex has no name, defaulting to %s" % index_names[i])
    index_tz = [get_timezone(i.tz) if isinstance(i, DatetimeIndex) else None for i in index.levels]
    return ix_vals, index_names, index_tz
Пример #8
0
def _multi_index_to_records(index, empty_index):
    # array of tuples to numpy cols. copy copy copy
    if not empty_index:
        ix_vals = list(map(np.array, [index.get_level_values(i) for i in range(index.nlevels)]))
    else:
        # empty multi index has no size, create empty arrays for recarry.
        ix_vals = [np.array([]) for n in index.names]
    index_names = list(index.names)
    count = 0
    for i, n in enumerate(index_names):
        if n is None:
            index_names[i] = 'level_%d' % count
            count += 1
            log.info("Level in MultiIndex has no name, defaulting to %s" % index_names[i])
    index_tz = [get_timezone(i.tz) if isinstance(i, DatetimeIndex) else None for i in index.levels]
    return ix_vals, index_names, index_tz
Пример #9
0
    def _index_to_records(self, df):
        metadata = {}
        index = df.index
        index_tz = None

        if isinstance(index, MultiIndex):
            ix_vals, index_names, index_tz = _multi_index_to_records(index, len(df) == 0)
        else:
            ix_vals = [index.values]
            index_names = list(index.names)
            if index_names[0] is None:
                index_names = ['index']
                log.info("Index has no name, defaulting to 'index'")
            if isinstance(index, DatetimeIndex) and index.tz is not None:
                index_tz = get_timezone(index.tz)

        if index_tz is not None:
            metadata['index_tz'] = index_tz
        metadata['index'] = index_names

        return index_names, ix_vals, metadata
Пример #10
0
    def _index_to_records(self, df):
        metadata = {}
        index = df.index
        index_tz = None

        if isinstance(index, MultiIndex):
            ix_vals, index_names, index_tz = _multi_index_to_records(index, len(df) == 0)
        else:
            ix_vals = [index.values]
            index_names = list(index.names)
            if index_names[0] is None:
                index_names = ['index']
                log.info("Index has no name, defaulting to 'index'")
            if isinstance(index, DatetimeIndex) and index.tz is not None:
                index_tz = get_timezone(index.tz)

        if index_tz is not None:
            metadata['index_tz'] = index_tz
        metadata['index'] = index_names

        return index_names, ix_vals, metadata
Пример #11
0
 def test_utc_z_designator(self):
     assert get_timezone(Timestamp('2014-11-02 01:00Z').tzinfo) == 'UTC'
Пример #12
0
 def _timezone(self):
     """ Comparable timezone both for pytz / dateutil"""
     return timezones.get_timezone(self.tzinfo)
Пример #13
0
 def test_utc_z_designator(self):
     assert get_timezone(Timestamp('2014-11-02 01:00Z').tzinfo) == 'UTC'
Пример #14
0
 def test_utc_z_designator(self):
     assert get_timezone(Timestamp("2014-11-02 01:00Z").tzinfo) is utc
Пример #15
0
 def _timezone(self):
     """ Comparable timezone both for pytz / dateutil"""
     return timezones.get_timezone(self.tzinfo)