def _convert_index(index): # Let's assume the index is homogeneous values = np.asarray(index) if isinstance(values[0], (datetime, date)): if isinstance(values[0], datetime): kind = 'datetime' else: kind = 'date' converted = np.array([time.mktime(v.timetuple()) for v in values], dtype=np.int64) return converted, kind, _tables().Time64Col() elif isinstance(values[0], basestring): converted = np.array(list(values), dtype=np.str_) itemsize = converted.dtype.itemsize return converted, 'string', _tables().StringCol(itemsize) elif com.is_integer(values[0]): # take a guess for now, hope the values fit atom = _tables().Int64Col() return np.asarray(values, dtype=np.int64), 'integer', atom elif com.is_float(values[0]): atom = _tables().Float64Col() return np.asarray(values, dtype=np.float64), 'float', atom else: # pragma: no cover atom = _tables().ObjectAtom() return np.asarray(values, dtype='O'), 'object', atom
def _maybe_cast_slice_bound(self, label, side, kind): """ If label is a string, cast it to timedelta according to resolution. Parameters ---------- label : object side : {'left', 'right'} kind : string / None Returns ------- label : object """ if isinstance(label, compat.string_types): parsed = _coerce_scalar_to_timedelta_type(label, box=True) lbound = parsed.round(parsed.resolution) if side == "left": return lbound else: return lbound + _resolution_map[parsed.resolution]() - Timedelta(1, "ns") elif is_integer(label) or is_float(label): self._invalid_indexer("slice", label) return label
def convert(values, unit, axis): from pandas.tseries.index import DatetimeIndex def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, pydt.time): return dates.date2num(values) elif (com.is_integer(values) or com.is_float(values)): return values elif isinstance(values, basestring): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray)): if not isinstance(values, np.ndarray): values = np.array(values, dtype='O') try: values = tools.to_datetime(values) if isinstance(values, Index): values = values.map(_dt_to_float_ordinal) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: pass return values
def _format_label(x, precision=3): fmt_str = '%%.%dg' % precision if np.isinf(x): return str(x) elif com.is_float(x): frac, whole = np.modf(x) sgn = '-' if x < 0 else '' whole = abs(whole) if frac != 0.0: val = fmt_str % frac # rounded up or down if '.' not in val: if x < 0: return '%d' % (-whole - 1) else: return '%d' % (whole + 1) if 'e' in val: return _trim_zeros(fmt_str % x) else: val = _trim_zeros(val) if '.' in val: return sgn + '.'.join(('%d' % whole, val.split('.')[1])) else: # pragma: no cover return sgn + '.'.join(('%d' % whole, val)) else: return sgn + '%0.f' % whole else: return str(x)
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 : string / None Returns ------- bound : Period or object Notes ----- Value of `side` parameter should be validated in caller. """ 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
def _wrap_results(result, dtype): """ wrap our results if needed """ if issubclass(dtype.type, np.datetime64): if not isinstance(result, np.ndarray): result = lib.Timestamp(result) else: result = result.view(dtype) elif issubclass(dtype.type, np.timedelta64): if not isinstance(result, np.ndarray): # this is a scalar timedelta result! # we have series convert then take the element (scalar) # as series will do the right thing in py3 (and deal with numpy # 1.6.2 bug in that it results dtype of timedelta64[us] from pandas import Series # coerce float to results if is_float(result): result = int(result) result = Series([result], dtype='timedelta64[ns]') else: result = result.view(dtype) return result
def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, year=None, month=None, quarter=None, day=None, hour=None, minute=None, second=None): if isinstance(freq, Period): freq = freq.freq else: freq = _freq_mod.get_standard_freq(freq) if periods is not None: if com.is_float(periods): periods = int(periods) elif not com.is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if data is None: if ordinal is not None: data = np.asarray(ordinal, dtype=np.int64) else: fields = [year, month, quarter, day, hour, minute, second] data, freq = cls._generate_range(start, end, periods, freq, fields) else: ordinal, freq = cls._from_arraylike(data, freq) data = np.array(ordinal, dtype=np.int64, copy=False) subarr = data.view(cls) subarr.name = name subarr.freq = freq return subarr
def convert(values, unit, axis): from pandas.tseries.index import DatetimeIndex def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, pydt.time): return dates.date2num(values) elif com.is_integer(values) or com.is_float(values): return values elif isinstance(values, str): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray)): if not isinstance(values, np.ndarray): values = np.array(values, dtype="O") try: values = tools.to_datetime(values) if isinstance(values, Index): values = values.map(_dt_to_float_ordinal) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: pass return values
def __new__( cls, data=None, ordinal=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, tz=None, **kwargs ): freq = frequencies.get_standard_freq(freq) if periods is not None: if com.is_float(periods): periods = int(periods) elif not com.is_integer(periods): raise ValueError("Periods must be a number, got %s" % str(periods)) if data is None: if ordinal is not None: data = np.asarray(ordinal, dtype=np.int64) else: data, freq = cls._generate_range(start, end, periods, freq, kwargs) else: ordinal, freq = cls._from_arraylike(data, freq, tz) data = np.array(ordinal, dtype=np.int64, copy=False) return cls._simple_new(data, name=name, freq=freq)
def _convert_scalar_indexer(self, key, kind=None): """ we don't allow integer or float indexing on datetime-like when using loc Parameters ---------- key : label of the slice bound kind : {'ix', 'loc', 'getitem', 'iloc'} or None """ assert kind in ['ix', 'loc', 'getitem', 'iloc', None] # we don't allow integer/float indexing for loc # we don't allow float indexing for ix/getitem if lib.isscalar(key): is_int = is_integer(key) is_flt = is_float(key) if kind in ['loc'] and (is_int or is_flt): self._invalid_indexer('index', key) elif kind in ['ix', 'getitem'] and is_flt: self._invalid_indexer('index', key) return (super(DatetimeIndexOpsMixin, self) ._convert_scalar_indexer(key, kind=kind))
def convert(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, pydt.time): return dates.date2num(values) elif (com.is_integer(values) or com.is_float(values)): return values elif isinstance(values, basestring): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray)): if not isinstance(values, np.ndarray): values = com._asarray_tuplesafe(values) if com.is_integer_dtype(values) or com.is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = values.map(_dt_to_float_ordinal) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: pass return values
def _maybe_cast_slice_bound(self, label, side, kind): """ This function should be overloaded in subclasses that allow non-trivial casting on label-slice bounds, e.g. datetime-like indices allowing strings containing formatted datetimes. Parameters ---------- label : object side : {'left', 'right'} kind : string / None Returns ------- label : object Notes ----- Value of `side` parameter should be validated in caller. """ # we are a numeric index, so we accept # integer/floats directly if not (com.is_integer(label) or com.is_float(label)): self._invalid_indexer('slice', label) return label
def _convert_scalar_indexer(self, key, kind=None): """ we don't allow integer or float indexing on datetime-like when using loc Parameters ---------- key : label of the slice bound kind : {'ix', 'loc', 'getitem', 'iloc'} or None """ assert kind in ['ix', 'loc', 'getitem', 'iloc', None] # we don't allow integer/float indexing for loc # we don't allow float indexing for ix/getitem if lib.isscalar(key): is_int = is_integer(key) is_flt = is_float(key) if kind in ['loc'] and (is_int or is_flt): self._invalid_indexer('index', key) elif kind in ['ix', 'getitem'] and is_flt: self._invalid_indexer('index', key) return (super(DatetimeIndexOpsMixin, self)._convert_scalar_indexer(key, kind=kind))
def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, year=None, month=None, quarter=None, day=None, hour=None, minute=None, second=None): freq = _freq_mod.get_standard_freq(freq) if periods is not None: if com.is_float(periods): periods = int(periods) elif not com.is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if data is None: if ordinal is not None: data = np.asarray(ordinal, dtype=np.int64) else: fields = [year, month, quarter, day, hour, minute, second] data, freq = cls._generate_range(start, end, periods, freq, fields) else: ordinal, freq = cls._from_arraylike(data, freq) data = np.array(ordinal, dtype=np.int64, copy=False) subarr = data.view(cls) subarr.name = name subarr.freq = freq return subarr
def _maybe_cast_slice_bound(self, label, side, kind): """ If label is a string, cast it to timedelta according to resolution. Parameters ---------- label : object side : {'left', 'right'} kind : {'ix', 'loc', 'getitem'} Returns ------- label : object """ assert kind in ['ix', 'loc', 'getitem', None] if isinstance(label, compat.string_types): parsed = _coerce_scalar_to_timedelta_type(label, box=True) lbound = parsed.round(parsed.resolution) if side == 'left': return lbound else: return (lbound + to_offset(parsed.resolution) - Timedelta(1, 'ns')) elif is_integer(label) or is_float(label): self._invalid_indexer('slice', label) return label
def convert(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, pydt.time): return dates.date2num(values) elif (com.is_integer(values) or com.is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray)): if not isinstance(values, np.ndarray): values = com._asarray_tuplesafe(values) if com.is_integer_dtype(values) or com.is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = values.map(_dt_to_float_ordinal) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: pass return values
def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, tz=None, **kwargs): freq = frequencies.get_standard_freq(freq) if periods is not None: if is_float(periods): periods = int(periods) elif not is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if data is None: if ordinal is not None: data = np.asarray(ordinal, dtype=np.int64) else: data, freq = cls._generate_range(start, end, periods, freq, kwargs) else: ordinal, freq = cls._from_arraylike(data, freq, tz) data = np.array(ordinal, dtype=np.int64, copy=False) return cls._simple_new(data, name=name, freq=freq)
def _convert_index(index): inferred_type = lib.infer_dtype(index) # Let's assume the index is homogeneous values = np.asarray(index) if inferred_type == 'datetime64': converted = values.view('i8') return converted, 'datetime64', _tables().Int64Col() elif isinstance(values[0], datetime): converted = np.array([(time.mktime(v.timetuple()) + v.microsecond / 1E6) for v in values], dtype=np.float64) return converted, 'datetime', _tables().Time64Col() elif isinstance(values[0], date): converted = np.array([time.mktime(v.timetuple()) for v in values], dtype=np.int32) return converted, 'date', _tables().Time32Col() elif isinstance(values[0], basestring): converted = np.array(list(values), dtype=np.str_) itemsize = converted.dtype.itemsize return converted, 'string', _tables().StringCol(itemsize) elif com.is_integer(values[0]): # take a guess for now, hope the values fit atom = _tables().Int64Col() return np.asarray(values, dtype=np.int64), 'integer', atom elif com.is_float(values[0]): atom = _tables().Float64Col() return np.asarray(values, dtype=np.float64), 'float', atom else: # pragma: no cover atom = _tables().ObjectAtom() return np.asarray(values, dtype='O'), 'object', atom
def _get_string_slice(self, key, use_lhs=True, use_rhs=True): freq = getattr(self, 'freqstr', getattr(self, 'inferred_freq', None)) if is_integer(key) or is_float(key): self._invalid_indexer('slice', key) loc = self._partial_td_slice(key, freq, use_lhs=use_lhs, use_rhs=use_rhs) return loc
def convert(value, unit, axis): valid_types = (str, pydt.time) if isinstance(value, valid_types) or com.is_integer(value) or com.is_float(value): return time2num(value) if isinstance(value, Index): return value.map(time2num) if isinstance(value, (list, tuple, np.ndarray)): return [time2num(x) for x in value] return value
def convert(value, unit, axis): valid_types = (str, pydt.time) if (isinstance(value, valid_types) or com.is_integer(value) or com.is_float(value)): return time2num(value) if isinstance(value, Index): return value.map(time2num) if isinstance(value, (list, tuple, np.ndarray)): return [time2num(x) for x in value] return value
def get_datevalue(date, freq): if isinstance(date, Period): return date.asfreq(freq).ordinal elif isinstance(date, (str, datetime, pydt.date, pydt.time)): return Period(date, freq).ordinal elif com.is_integer(date) or com.is_float(date) or (isinstance(date, np.ndarray) and (date.size == 1)): return date elif date is None: return None raise ValueError("Unrecognizable date '%s'" % date)
def _ensure_numeric(x): if isinstance(x, np.ndarray): if x.dtype == np.object_: x = x.astype(np.float64) elif not (com.is_float(x) or com.is_integer(x)): try: x = float(x) except Exception: raise TypeError('Could not convert %s to numeric' % str(x)) return x
def get_datevalue(date, freq): if isinstance(date, Period): return date.asfreq(freq).ordinal elif isinstance(date, (str, datetime, pydt.date, pydt.time)): return Period(date, freq).ordinal elif (com.is_integer(date) or com.is_float(date) or (isinstance(date, np.ndarray) and (date.size == 1))): return date elif date is None: return None raise ValueError("Unrecognizable date '%s'" % date)
def convert(values, units, axis): if not hasattr(axis, "freq"): raise TypeError("Axis must have `freq` set to convert to Periods") valid_types = (str, datetime, Period, pydt.date, pydt.time) if isinstance(values, valid_types) or com.is_integer(values) or com.is_float(values): return get_datevalue(values, axis.freq) if isinstance(values, Index): return values.map(lambda x: get_datevalue(x, axis.freq)) if isinstance(values, (list, tuple, np.ndarray)): return [get_datevalue(x, axis.freq) for x in values] return values
def _conv_value(val): # Convert numpy types to Python types for the Excel writers. if com.is_integer(val): val = int(val) elif com.is_float(val): val = float(val) elif com.is_bool(val): val = bool(val) elif isinstance(val, Period): val = "%s" % val return val
def convert(values, units, axis): if not hasattr(axis, 'freq'): raise TypeError('Axis must have `freq` set to convert to Periods') valid_types = (str, datetime, Period, pydt.date, pydt.time) if (isinstance(values, valid_types) or com.is_integer(values) or com.is_float(values)): return get_datevalue(values, axis.freq) if isinstance(values, Index): return values.map(lambda x: get_datevalue(x, axis.freq)) if isinstance(values, (list, tuple, np.ndarray)): return [get_datevalue(x, axis.freq) for x in values] return values
def _convert_scalar_indexer(self, key, kind=None): """ we don't allow integer or float indexing on datetime-like when using loc Parameters ---------- key : label of the slice bound kind : optional, type of the indexing operation (loc/ix/iloc/None) """ if kind in ['loc'] and lib.isscalar(key) and (is_integer(key) or is_float(key)): self._invalid_indexer('index',key) return super(DatetimeIndexOpsMixin, self)._convert_scalar_indexer(key, kind=kind)
def convert(values, units, axis): if not hasattr(axis, 'freq'): raise TypeError('Axis must have `freq` set to convert to Periods') valid_types = (str, datetime, Period, pydt.date, pydt.time) if (isinstance(values, valid_types) or com.is_integer(values) or com.is_float(values)): return get_datevalue(values, axis.freq) if isinstance(values, PeriodIndex): return values.asfreq(axis.freq).values if isinstance(values, Index): return values.map(lambda x: get_datevalue(x, axis.freq)) if isinstance(values, (list, tuple, np.ndarray, Index)): return PeriodIndex(values, freq=axis.freq).values return values
def convert(cls, values, units, axis): def try_parse(values): try: return datetools.to_datetime(values).toordinal() except Exception: return values if (com.is_integer(values) or com.is_float(values)): return values elif isinstance(values, str): return try_parse(values) elif isinstance(values, Index): return values.map(try_parse) return map(try_parse, values)
def _simple_new(cls, data, sp_index, fill_value): if (com.is_integer_dtype(data) and com.is_float(fill_value) and sp_index.ngaps > 0): # if float fill_value is being included in dense repr, # convert values to float data = data.astype(float) result = data.view(cls) if not isinstance(sp_index, SparseIndex): # caller must pass SparseIndex raise ValueError('sp_index must be a SparseIndex') result.sp_index = sp_index result.fill_value = fill_value return result
def convert(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, pydt.time): return dates.date2num(values) elif (com.is_integer(values) or com.is_float(values)): return values elif isinstance(values, str): return try_parse(values) elif isinstance(values, Index): return values.map(try_parse) elif isinstance(values, (list, tuple, np.ndarray)): return [try_parse(x) for x in values] return values
def _format_label(x, precision=3): fmt_str = '%%.%dg' % precision if com.is_float(x): frac, whole = np.modf(x) sgn = '-' if x < 0 else '' whole = abs(whole) if frac != 0.0: val = fmt_str % frac if 'e' in val: return _trim_zeros(fmt_str % x) else: val = _trim_zeros(val) if '.' in val: return sgn + '.'.join(('%d' % whole, val.split('.')[1])) else: return sgn + '.'.join(('%d' % whole, val)) else: return sgn + '%d' % whole else: return str(x)
def _ensure_numeric(x): if isinstance(x, np.ndarray): if is_integer_dtype(x) or is_bool_dtype(x): x = x.astype(np.float64) elif is_object_dtype(x): try: x = x.astype(np.complex128) except: x = x.astype(np.float64) else: if not np.any(x.imag): x = x.real elif not (is_float(x) or is_integer(x) or is_complex(x)): try: x = float(x) except Exception: try: x = complex(x) except Exception: raise TypeError('Could not convert %s to numeric' % str(x)) return x
def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, tz=None, **kwargs): if periods is not None: if is_float(periods): periods = int(periods) elif not is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if name is None and hasattr(data, 'name'): name = data.name if data is None: if ordinal is not None: data = np.asarray(ordinal, dtype=np.int64) else: data, freq = cls._generate_range(start, end, periods, freq, kwargs) else: ordinal, freq = cls._from_arraylike(data, freq, tz) data = np.array(ordinal, dtype=np.int64, copy=copy) return cls._simple_new(data, name=name, freq=freq)
def _convert_index(index): # Let's assume the index is homogeneous values = np.asarray(index) if isinstance(values[0], datetime): converted = np.array([(time.mktime(v.timetuple()) + v.microsecond / 1e6) for v in values], dtype=np.float64) return converted, "datetime", _tables().Time64Col() elif isinstance(values[0], date): converted = np.array([time.mktime(v.timetuple()) for v in values], dtype=np.int32) return converted, "date", _tables().Time32Col() elif isinstance(values[0], basestring): converted = np.array(list(values), dtype=np.str_) itemsize = converted.dtype.itemsize return converted, "string", _tables().StringCol(itemsize) elif com.is_integer(values[0]): # take a guess for now, hope the values fit atom = _tables().Int64Col() return np.asarray(values, dtype=np.int64), "integer", atom elif com.is_float(values[0]): atom = _tables().Float64Col() return np.asarray(values, dtype=np.float64), "float", atom else: # pragma: no cover atom = _tables().ObjectAtom() return np.asarray(values, dtype="O"), "object", atom
def test_is_float(self): self.assertTrue(com.is_float(1.1)) self.assertTrue(com.is_float(np.float64(1.1))) self.assertTrue(com.is_float(np.nan)) self.assertFalse(com.is_float(True)) self.assertFalse(com.is_float(1)) self.assertFalse(com.is_float(1 + 3j)) self.assertFalse(com.is_float(np.bool(False))) self.assertFalse(com.is_float(np.bool_(False))) self.assertFalse(com.is_float(np.int64(1))) self.assertFalse(com.is_float(np.complex128(1 + 3j))) self.assertFalse(com.is_float(None)) self.assertFalse(com.is_float('x')) self.assertFalse(com.is_float(datetime(2011, 1, 1))) self.assertFalse(com.is_float(np.datetime64('2011-01-01'))) self.assertFalse(com.is_float(pd.Timestamp('2011-01-01'))) self.assertFalse(com.is_float(pd.Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(com.is_float(timedelta(1000))) self.assertFalse(com.is_float(np.timedelta64(1, 'D'))) self.assertFalse(com.is_float(pd.Timedelta('1 days')))
def __new__(cls, data=None, unit=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, closed=None, verify_integrity=True, **kwargs): if isinstance(data, TimedeltaIndex) and freq is None and name is None: if copy: data = data.copy() return data freq_infer = False if not isinstance(freq, DateOffset): # if a passed freq is None, don't infer automatically if freq != 'infer': freq = to_offset(freq) else: freq_infer = True freq = None if periods is not None: if is_float(periods): periods = int(periods) elif not is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if data is None and freq is None: raise ValueError("Must provide freq argument if no data is " "supplied") if data is None: return cls._generate(start, end, periods, name, freq, closed=closed) if unit is not None: data = to_timedelta(data, unit=unit, box=False) if not isinstance(data, (np.ndarray, Index, ABCSeries)): if lib.isscalar(data): raise ValueError('TimedeltaIndex() must be called with a ' 'collection of some kind, %s was passed' % repr(data)) # convert if not already if getattr(data, 'dtype', None) != _TD_DTYPE: data = to_timedelta(data, unit=unit, box=False) elif copy: data = np.array(data, copy=True) # check that we are matching freqs if verify_integrity and len(data) > 0: if freq is not None and not freq_infer: index = cls._simple_new(data, name=name) inferred = index.inferred_freq if inferred != freq.freqstr: on_freq = cls._generate( index[0], None, len(index), name, freq) if not np.array_equal(index.asi8, on_freq.asi8): raise ValueError('Inferred frequency {0} from passed ' 'timedeltas does not conform to ' 'passed frequency {1}' .format(inferred, freq.freqstr)) index.freq = freq return index if freq_infer: index = cls._simple_new(data, name=name) inferred = index.inferred_freq if inferred: index.freq = to_offset(inferred) return index return cls._simple_new(data, name=name, freq=freq)
def _is_valid_index(x): return (com.is_integer(x) or com.is_float(x) and np.allclose(x, int(x), rtol=_eps, atol=0))
def _format_value(self, val): if lib.checknull(val): val = self.na_rep if self.float_format is not None and com.is_float(val): val = float(self.float_format % val) return val
def _is_valid_index(x): return com.is_float(x)
def default_display_func(x): if com.is_float(x): return '{:>.{precision}g}'.format(x, precision=self.precision) else: return x
def __new__(cls, data=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, tz=None, verify_integrity=True, normalize=False, **kwds): dayfirst = kwds.pop('dayfirst', None) yearfirst = kwds.pop('yearfirst', None) warn = False if 'offset' in kwds and kwds['offset']: freq = kwds['offset'] warn = True freq_infer = False if not isinstance(freq, DateOffset): if freq != 'infer': freq = to_offset(freq) else: freq_infer = True freq = None if warn: import warnings warnings.warn( "parameter 'offset' is deprecated, " "please use 'freq' instead", FutureWarning) offset = freq if periods is not None: if com.is_float(periods): periods = int(periods) elif not com.is_integer(periods): raise ValueError('Periods must be a number, got %s' % str(periods)) if data is None and offset is None: raise ValueError("Must provide freq argument if no data is " "supplied") if data is None: return cls._generate(start, end, periods, name, offset, tz=tz, normalize=normalize) if not isinstance(data, np.ndarray): if np.isscalar(data): raise ValueError('DatetimeIndex() must be called with a ' 'collection of some kind, %s was passed' % repr(data)) # other iterable of some kind if not isinstance(data, (list, tuple)): data = list(data) data = np.asarray(data, dtype='O') # try a few ways to make it datetime64 if lib.is_string_array(data): data = _str_to_dt_array(data, offset, dayfirst=dayfirst, yearfirst=yearfirst) else: data = tools.to_datetime(data) data.offset = offset if isinstance(data, DatetimeIndex): if name is not None: data.name = name return data if issubclass(data.dtype.type, basestring): subarr = _str_to_dt_array(data, offset, dayfirst=dayfirst, yearfirst=yearfirst) elif issubclass(data.dtype.type, np.datetime64): if isinstance(data, DatetimeIndex): if tz is None: tz = data.tz subarr = data.values if offset is None: offset = data.offset verify_integrity = False else: if data.dtype != _NS_DTYPE: subarr = lib.cast_to_nanoseconds(data) else: subarr = data elif data.dtype == _INT64_DTYPE: if isinstance(data, Int64Index): raise TypeError('cannot convert Int64Index->DatetimeIndex') if copy: subarr = np.asarray(data, dtype=_NS_DTYPE) else: subarr = data.view(_NS_DTYPE) else: try: subarr = tools.to_datetime(data) except ValueError: # tz aware subarr = tools.to_datetime(data, utc=True) if not np.issubdtype(subarr.dtype, np.datetime64): raise TypeError('Unable to convert %s to datetime dtype' % str(data)) if isinstance(subarr, DatetimeIndex): if tz is None: tz = subarr.tz else: if tz is not None: tz = tools._maybe_get_tz(tz) if (not isinstance(data, DatetimeIndex) or getattr(data, 'tz', None) is None): # Convert tz-naive to UTC ints = subarr.view('i8') subarr = lib.tz_localize_to_utc(ints, tz) subarr = subarr.view(_NS_DTYPE) subarr = subarr.view(cls) subarr.name = name subarr.offset = offset subarr.tz = tz if verify_integrity and len(subarr) > 0: if offset is not None and not freq_infer: inferred = subarr.inferred_freq if inferred != offset.freqstr: raise ValueError('Dates do not conform to passed ' 'frequency') if freq_infer: inferred = subarr.inferred_freq if inferred: subarr.offset = to_offset(inferred) return subarr