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
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
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
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
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
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
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
def test_utc_z_designator(self): assert get_timezone(Timestamp('2014-11-02 01:00Z').tzinfo) == 'UTC'
def _timezone(self): """ Comparable timezone both for pytz / dateutil""" return timezones.get_timezone(self.tzinfo)
def test_utc_z_designator(self): assert get_timezone(Timestamp("2014-11-02 01:00Z").tzinfo) is utc